Passed
Push — master ( c46382...0c57e9 )
by Emanuele
02:05
created

EnableFeatureCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ae\FeatureBundle\Command;
4
5
use Ae\FeatureBundle\Entity\FeatureManager;
6
use Doctrine\ORM\EntityManager;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Command used to enable an existing feature.
15
 *
16
 * @author Emanuele Minotto <[email protected]>
17
 */
18 View Code Duplication
class EnableFeatureCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
19
{
20
    /**
21
     * @var FeatureManager
22
     */
23
    private $featureManager;
24
25
    /**
26
     * @var EntityManager
27
     */
28
    private $entityManager;
29
30
    /**
31
     * @param FeatureManager $featureManager
32
     * @param EntityManager  $entityManager
33
     */
34 2
    public function __construct(
35
        FeatureManager $featureManager,
36
        EntityManager $entityManager
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
37
    ) {
38 2
        parent::__construct();
39
40 2
        $this->featureManager = $featureManager;
41 2
        $this->entityManager = $entityManager;
42 2
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    protected function configure()
48
    {
49 2
        $this
50 2
            ->setName('features:enable')
51 2
            ->setDescription('Enable an existing feature')
52 2
            ->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
53 2
            ->addArgument('name', InputArgument::REQUIRED, 'Feature name')
54 2
            ->addOption(
55 2
                'role',
56 2
                null,
57 2
                InputOption::VALUE_REQUIRED,
58
                'The feature will be only for a role'
59 2
            );
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function execute(InputInterface $input, OutputInterface $output)
66
    {
67 2
        $name = $input->getArgument('name');
68 2
        $parent = $input->getArgument('parent');
69
70 2
        $output->write(sprintf(
71 2
            'Enabling <info>%s</info>.<info>%s</info>... ',
72 2
            $parent,
73
            $name
74 2
        ));
75
76 2
        $feature = $this->featureManager->find($name, $parent);
77
78 2
        $feature->setEnabled(true);
79
80 2
        if (null !== $input->getOption('role')) {
81 1
            $feature->setRole($input->getOption('role'));
82 1
        }
83
84 2
        $this->entityManager->persist($feature);
85 2
        $this->entityManager->flush();
86
87 2
        $this->featureManager->emptyCache($name, $parent);
88
89 2
        $output->writeln('OK');
90 2
    }
91
}
92