1 | <?php |
||
2 | |||
3 | namespace GGGGino\SkuskuCartBundle\Command; |
||
4 | |||
5 | use Doctrine\Common\Annotations\AnnotationReader; |
||
6 | use Doctrine\ORM\EntityManager; |
||
7 | use Doctrine\ORM\Mapping\Table; |
||
8 | use GGGGino\SkuskuCartBundle\Service\CartManager; |
||
9 | use GGGGino\SkuskuCartBundle\Service\SkuskuHelper; |
||
10 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
11 | use Symfony\Component\Console\Input\InputArgument; |
||
12 | use Symfony\Component\Console\Input\InputInterface; |
||
13 | use Symfony\Component\Console\Input\InputOption; |
||
14 | use Symfony\Component\Console\Output\OutputInterface; |
||
15 | use Symfony\Component\Console\Question\ChoiceQuestion; |
||
16 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
17 | use Symfony\Component\Console\Question\Question; |
||
18 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
19 | |||
20 | /** |
||
21 | * Class CurrencyCreateCommand |
||
22 | * @package GGGGino\SkuskuCartBundle\Command |
||
23 | */ |
||
24 | class CurrencyCreateCommand extends ContainerAwareCommand |
||
25 | { |
||
26 | /** |
||
27 | * @var SkuskuHelper |
||
28 | */ |
||
29 | private $skuskuHelper; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public function configure() |
||
35 | { |
||
36 | $this |
||
37 | ->setName('ggggino_skusku:currency:create') |
||
38 | ->setDescription('Create a Currency') |
||
39 | ->addArgument('entity', InputArgument::OPTIONAL, 'The fully qualified model class'); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | protected function interact(InputInterface $input, OutputInterface $output) |
||
46 | { |
||
47 | $this->skuskuHelper = $this->getContainer()->get(SkuskuHelper::class); |
||
48 | |||
49 | /** @var array $abstractEntities */ |
||
50 | $abstractEntities = $this->skuskuHelper->getAbstractEntities(); |
||
51 | $abstractEntitiesInverted = array_flip($abstractEntities); |
||
52 | |||
53 | $helper = $this->getHelper('question'); |
||
54 | |||
55 | // If i make a mistake writing the class this enforce you to chose |
||
56 | // @todo now i'm forced to write thee entity namespace wth 2 backslash otherwise it will be removed |
||
57 | if ( !in_array($input->getArgument('entity'), $abstractEntities) ) { |
||
58 | $output->writeln('<error>Entity: ' . $input->getArgument('entity') . ' Not found</error>'); |
||
59 | $question = new ChoiceQuestion( |
||
60 | 'Please select the entity', |
||
61 | array_keys($abstractEntitiesInverted) |
||
62 | ); |
||
63 | $question->setErrorMessage('Entity %s is invalid.'); |
||
64 | |||
65 | $concreteClass = $helper->ask($input, $output, $question); |
||
66 | |||
67 | $input->setArgument('entity', $concreteClass); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | protected function execute(InputInterface $input, OutputInterface $output) |
||
75 | { |
||
76 | $entityClass = $input->getArgument('entity'); |
||
77 | $classReflected = new \ReflectionClass($entityClass); |
||
78 | $entityInstance = new $entityClass(); |
||
79 | |||
80 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
81 | |||
82 | $helper = $this->getHelper('question'); |
||
83 | |||
84 | $reader = new AnnotationReader(); |
||
85 | $classAnnotated = $reader->getClassAnnotation($classReflected, Table::class); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
86 | |||
87 | $classReflectedProps = $classReflected->getProperties(); |
||
88 | |||
89 | foreach($classReflectedProps as $key => $value) { |
||
90 | $question = new Question('Set value for (' . $value->name . '): ', false); |
||
91 | |||
92 | $response = $helper->ask($input, $output, $question); |
||
93 | if( !$response ){ |
||
94 | continue; |
||
95 | } |
||
96 | $propertyAccessor->setValue($entityInstance, $value->name, $response); |
||
97 | } |
||
98 | |||
99 | /** @var EntityManager $em */ |
||
100 | $em = $this->getContainer()->get('doctrine')->getManager(); |
||
101 | $em->persist($entityInstance); |
||
102 | $em->flush(); |
||
103 | |||
104 | $output->writeln('Entity created'); |
||
105 | } |
||
106 | } |