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

DisableFeatureCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
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\Output\OutputInterface;
11
12
/**
13
 * Command used to disable an existing feature.
14
 *
15
 * @author Emanuele Minotto <[email protected]>
16
 */
17
class DisableFeatureCommand extends Command
18
{
19
    /**
20
     * @var FeatureManager
21
     */
22
    private $featureManager;
23
24
    /**
25
     * @var EntityManager
26
     */
27
    private $entityManager;
28
29
    /**
30
     * @param FeatureManager $featureManager
31
     * @param EntityManager  $entityManager
32
     */
33 1
    public function __construct(
34
        FeatureManager $featureManager,
35
        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...
36
    ) {
37 1
        parent::__construct();
38
39 1
        $this->featureManager = $featureManager;
40 1
        $this->entityManager = $entityManager;
41 1
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    protected function configure()
47
    {
48 1
        $this
49 1
            ->setName('features:disable')
50 1
            ->setDescription('Disable an existing feature')
51 1
            ->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
52 1
            ->addArgument('name', InputArgument::REQUIRED, 'Feature name');
53 1
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function execute(InputInterface $input, OutputInterface $output)
59
    {
60 1
        $name = $input->getArgument('name');
61 1
        $parent = $input->getArgument('parent');
62
63 1
        $output->write(sprintf(
64 1
            'Disabling <info>%s</info>.<info>%s</info>... ',
65 1
            $parent,
66
            $name
67 1
        ));
68
69 1
        $feature = $this->featureManager->find($name, $parent);
70
71 1
        $feature->setEnabled(false);
72
73 1
        $this->entityManager->persist($feature);
74 1
        $this->entityManager->flush();
75
76 1
        $this->featureManager->emptyCache($name, $parent);
77
78 1
        $output->writeln('OK');
79 1
    }
80
}
81