1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Loevgaard\DandomainConsignmentBundle\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\OptimisticLockException; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentManufacturerException; |
10
|
|
|
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
View Code Duplication |
class EnableCommand extends ContainerAwareCommand |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ManufacturerRepository |
20
|
|
|
*/ |
21
|
|
|
protected $manufacturerRepository; |
22
|
|
|
|
23
|
|
|
public function __construct(ManufacturerRepository $manufacturerRepository) |
24
|
|
|
{ |
25
|
|
|
$this->manufacturerRepository = $manufacturerRepository; |
26
|
|
|
|
27
|
|
|
parent::__construct(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName('loevgaard:dandomain-consignment:enable') |
34
|
|
|
->setDescription('Enables consignment for the specified manufacturer') |
35
|
|
|
->addArgument('manufacturer', InputArgument::REQUIRED, 'The manufacturer') |
36
|
|
|
; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param InputInterface $input |
41
|
|
|
* @param OutputInterface $output |
42
|
|
|
* |
43
|
|
|
* @return int|null|void |
44
|
|
|
* |
45
|
|
|
* @throws NonExistentManufacturerException |
46
|
|
|
* @throws ORMException |
47
|
|
|
* @throws OptimisticLockException |
48
|
|
|
*/ |
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
|
|
// fetch arguments and options |
52
|
|
|
$manufacturer = $input->getArgument('manufacturer'); |
53
|
|
|
|
54
|
|
|
// find manufacturer |
55
|
|
|
$manufacturer = $this->manufacturerRepository->findOneByExternalId($manufacturer); |
56
|
|
|
|
57
|
|
|
if (!$manufacturer) { |
58
|
|
|
throw new NonExistentManufacturerException('The manufacturer does not exist'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$manufacturer->setConsignment(true); |
62
|
|
|
$this->manufacturerRepository->flush(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.