Last active
April 15, 2018 09:20
-
-
Save l3l0/948184b461ad3e94c46e71e4a8bfb3c6 to your computer and use it in GitHub Desktop.
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 | |
| declare(strict_types=1); | |
| namespace Cocoders\core\Application\UseCase; | |
| use Cocoders\Core\Domain\Users; | |
| use Cocoders\Core\Application\TransactionManager; | |
| use Cocoders\core\Application\UseCase\RegisterUser\UserFactory; | |
| use Cocoders\core\Application\UseCase\RegisterUser\Command; | |
| class RegisterUser | |
| { | |
| private $transactionManager; | |
| private $users; | |
| private $factory; | |
| public function __construct(TransactionManager $manager, Users $users, UserFactory $factory) | |
| { | |
| $this->transactionManager = $manager; | |
| $this->users = $users; | |
| $this->factory = $factory; | |
| } | |
| public function execute(Command $command): void | |
| { | |
| $this->transactionManager->begin(); | |
| try { | |
| $this->users->add( | |
| $this->factory->create($user) | |
| ); | |
| $this->transactionManager->commit(); | |
| } catch (\Throwable $t) { | |
| $this->transactionManager->rollback(); | |
| throw $t; | |
| } | |
| } | |
| } |
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 | |
| declare(strict_types=1); | |
| namespace Cocoders\Core\Domain; | |
| class User | |
| { | |
| private $id; | |
| private $username; | |
| private $email; | |
| private $userProfile; | |
| public function __construct(UserId $id, string $username, Email $email, UserProfile $profile) | |
| { | |
| $this->id = $id; | |
| $this->username = $username; | |
| $this->email = $email; | |
| $this->profile = $profile; | |
| } | |
| } |
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 | |
| declare(strict_types=1); | |
| namespace Cocoders\Core\Domain; | |
| use Cocoders\Core\Domain\Exception\UserNotFound; | |
| interface Users | |
| { | |
| public function add(User $user): void; | |
| /** | |
| * @throws UserNotFound | |
| */ | |
| public function get(UserId $userId): User; | |
| } |
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 | |
| declare(strict_types=1); | |
| namespace Cocoders\Integration\Core\Domain; | |
| use Cocoders\Core\Domain\Exception\UserNotFound; | |
| use Cocoders\Core\Domain\User; | |
| use Doctrine\Common\Persistence\ObjectManager; | |
| use Cocoders\Core\Domain\Users as UsersInterface; | |
| final class Users implements UsersInterface | |
| { | |
| private $manager; | |
| public function __construct(ObjectManager $manager) | |
| { | |
| $this->manager = $manager; | |
| } | |
| public function add(User $user): void | |
| { | |
| $this->manager->persist($user); | |
| } | |
| public function get(UserId $userId): User | |
| { | |
| $user = $this->manager->getRepository(User::class)->find((string) $userId); | |
| if (!$user) { | |
| throw new UserNotFound(); | |
| } | |
| return $user; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment