Last active
August 29, 2015 14:23
-
-
Save fesor/a3d1182444ca6d927bca to your computer and use it in GitHub Desktop.
This file contains 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 AddAdvertPhotoCommand implements CommandInterface | |
{ | |
private $repository; | |
private $fileUploader; | |
public function __construct( | |
AdvertPhotoRepositoryInterface $repository, | |
FileUploaderInterface $fileUploader | |
) { | |
$this->repository = $repository; | |
$this->fileUploader = $fileUploader; | |
} | |
public function execute($dto = null) | |
{ | |
if (!($dto->advert instanceof Advert)) { | |
throw new LogicException('Incorrect object. Need '.Advert::class); | |
} | |
if (!isset($dto->photo)) { | |
throw new LogicException('Incorrect object. Need photo property'); | |
} | |
$photo = new AdvertPhoto($dto->advert, $this->fileUploader->upload($dto->photo, 'advert_photos')); | |
$this->repository->add($photo); | |
} | |
} |
This file contains 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 | |
final class AddAdvertPhotoCommand implements Command | |
{ | |
private $advert; | |
private $photo; | |
public function __construct(Advert $advert, File $photo) | |
{ | |
$this->advert = advert; | |
$this->photo = $photo; | |
} | |
public function getAdvert() { return $this->advert; } | |
public function getPhoto() { return $this->photo; } | |
} | |
final class AddAdvertPhotoHandler implements CommandHandler | |
{ | |
private $photoRepository; | |
private $fileUploader; | |
public function __construct(PhotoRepository $photoRepository, FileUploader $fileUploader) | |
{ | |
$this->photoRepostory = $phtoRepository; | |
$this->fileUploader = $fileUploader; | |
} | |
public function supports(Command $command) | |
{ | |
return $command instanceof AddAdvertPhotoCommand; | |
} | |
public function execute(Command $command) | |
{ | |
$photo = new AdvertPhoto( | |
$command->getAdvert(), | |
$this->fileUploader->upload($command->getPhoto(), 'advert_photos') | |
); | |
$this->photoRepository->add($photo); | |
return $photo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment