Created
August 14, 2014 15:46
-
-
Save MarkRedeman/27f9b7bc7b7f9d37b709 to your computer and use it in GitHub Desktop.
Authenticating your Command Bus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AddStudentGradeToCourseCommand { | |
public $studentId; | |
public $courseId; | |
public $grade; | |
public function __construct($studentId, $courseId, $grade) | |
{ | |
$this->studentId = $studentId; | |
$this->courseId = $courseId; | |
$this->grade = $grade; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Given a AddStudentGradeToCourseCommand we handle it by registering | |
// the new grade | |
use Acme\Grades\GradesRepository; | |
use Laracasts\Commander\Events\DispatchableTrait; | |
class AddStudentGradeToCourseHandler implements Handler { | |
use DispatchableTrait; | |
protected $grades; | |
public function __construct(GradesRepository $grades) | |
{ | |
$this->repo = $grades; | |
} | |
// We handle the adding of a new grade, the Grade::register() method | |
// will raise a couple of events such that we can easily notify | |
// students about their grades | |
public function handle(AddStudentGradeToCourseCommand $command) | |
{ | |
$grade = Grade::register($command->studentId, $command->courseId, $command->grade); | |
$this->repo->save($grade); | |
$this->dispatchEventsFor($grade); | |
return $grade; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Auth\AuthManager as Auth; | |
use Acme\Courses\CoursesRepository; | |
use Acme\Students\StudentsRepository; | |
class AddStudentGradeToCourseAuthenticator { | |
protected $courses; | |
protected $students; | |
protected $auth; | |
public function __construct(Auth $auth, CoursesRepository $courses, StudentsRepository $students) | |
{ | |
$this->auth = $auth; | |
$this->courses = $courses; | |
$this->students = $students; | |
} | |
public function authenticate(AddStudentGradeToCourseCommand $command) | |
{ | |
// Get the user who wants to add a grade | |
$user = $this->auth->user(); | |
// Get the course from the database with its associated teachers | |
$course = $this->courses->byIdWithTeachers($command->courseId); | |
// Check if the given course is teached by the authenticated user | |
$verification = $course->verifyCourseIsTaughtByUser($course, $user); | |
// If any domain events were raised, dispatch them | |
$this->dispatchEventsFor($course); | |
// We throw an exception if we couldn't verify that the user teaches | |
// the given course corresponding | |
if (! $verification) | |
{ | |
throw new NoPermissionException('User is not allowed to add grades to this course'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// A controller using the laracasts/commander package to execute commands | |
class GradesController extends BaseController { | |
public function store() | |
{ | |
$grade = $this->execute(AddStudentGradeToCourseCommand::class); | |
return $grade; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment