Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:23
Show Gist options
  • Save fesor/a3d1182444ca6d927bca to your computer and use it in GitHub Desktop.
Save fesor/a3d1182444ca6d927bca to your computer and use it in GitHub Desktop.
<?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);
}
}
<?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