Created
September 22, 2019 10:03
-
-
Save lolychank/eabb0898ed825d1b09d4c391fb8428a3 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 | |
declare(strict_types=1); | |
... | |
use Symfony\Component\Validator\Constraints as Assert; | |
class Command | |
{ | |
/** | |
* @var string | |
* | |
* @Assert\NotBlank(message="Нельзя создать пустой тег") | |
*/ | |
public $name; | |
/** | |
* @var string | |
* | |
* @Assert\NotBlank(message="У тега должна быть картинка.") | |
*/ | |
public $image; | |
} |
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 | |
... | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\FileType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class Form extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', TextType::class, ['label' => 'Имя']) | |
->add('image', FileType::class, ['label' => 'Изображение']); | |
} | |
/** | |
* @param OptionsResolver $resolver | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults([ | |
'data_class' => Command::class | |
]); | |
} | |
} |
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 Handler | |
{ | |
/** | |
* @var EntityManagerInterface | |
*/ | |
private $em; | |
/** | |
* @var TagRepository | |
*/ | |
private $tags; | |
public function __construct(EntityManagerInterface $em, TagRepository $tagsRepository) | |
{ | |
$this->em = $em; | |
$this->tags = $tagsRepository; | |
} | |
/** | |
* @param Command $command | |
* @throws \Exception | |
*/ | |
public function handle(Command $command): void | |
{ | |
if ($this->tags->findOneBy(['name' => $command->name])) { | |
throw new \DomainException('Такой тег уже существует'); | |
} | |
$tag = new Tag($command->name, $command->image); | |
$this->tags->add($tag); | |
$this->em->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment