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

CreateFeatureCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 6
dl 77
loc 77
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A configure() 20 20 1
A execute() 23 23 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 create a feature.
15
 *
16
 * @author Emanuele Minotto <[email protected]>
17
 */
18 View Code Duplication
class CreateFeatureCommand 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:create')
51 2
            ->setDescription('Create a new feature')
52 2
            ->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
53 2
            ->addArgument('name', InputArgument::REQUIRED, 'Feature name')
54 2
            ->addOption(
55 2
                'enabled',
56 2
                null,
57 2
                InputOption::VALUE_NONE,
58
                'The feature will be created as enabled'
59 2
            )
60 2
            ->addOption(
61 2
                'role',
62 2
                null,
63 2
                InputOption::VALUE_REQUIRED,
64
                'The feature will be only for a role'
65 2
            );
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function execute(InputInterface $input, OutputInterface $output)
72
    {
73 2
        $name = $input->getArgument('name');
74 2
        $parent = $input->getArgument('parent');
75
76 2
        $output->write(sprintf(
77 2
            'Creating <info>%s</info>.<info>%s</info>... ',
78 2
            $parent,
79
            $name
80 2
        ));
81
82 2
        $feature = $this->featureManager->findOrCreate($name, $parent);
83
84 2
        $feature->setEnabled($input->getOption('enabled'));
85 2
        $feature->setRole($input->getOption('role'));
86
87 2
        $this->entityManager->persist($feature);
88 2
        $this->entityManager->flush();
89
90 2
        $this->featureManager->emptyCache($name, $parent);
91
92 2
        $output->writeln('OK');
93 2
    }
94
}
95