Passed
Push — master ( 767439...9ba489 )
by Andrea
65:09 queued 34:45
created

BiCoreBundleClonaruoloCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 57
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 6.0289

Importance

Changes 0
Metric Value
cc 6
eloc 43
nc 6
nop 2
dl 0
loc 57
ccs 39
cts 43
cp 0.907
crap 6.0289
rs 8.6097
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cdf\BiCoreBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Doctrine\Common\Persistence\ObjectManager;
10
11
class BiCoreBundleClonaruoloCommand extends Command
12
{
13
    protected static $defaultName = 'bicorebundle:clonaruolo';
14
    private $em;
15
16 1
    protected function configure()
17
    {
18
        $this
19 1
                ->setDescription('Clona i permessi di un ruolo esistente su un nuovo ruolo')
20 1
                ->setHelp('Specificare come parametri il nome del ruolo da clonare e quello nuovo')
21 1
                ->addArgument('ruoloesistente', InputArgument::REQUIRED, 'Ruolo esistente')
22 1
                ->addArgument('nuovoruolo', InputArgument::REQUIRED, 'Nuovo ruolo')
23
        ;
24 1
    }
25
26 1
    public function __construct(ObjectManager $em)
27
    {
28 1
        $this->em = $em;
29
30
        // you *must* call the parent constructor
31 1
        parent::__construct();
32 1
    }
33
34 1
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36 1
        $ruoloesistente = $input->getArgument('ruoloesistente');
37 1
        $nuovoruolo = $input->getArgument('nuovoruolo');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
38
39 1
        if (!$ruoloesistente) {
40
            throw new \Exception('Inserire il ruolo da clonare');
41
        }
42 1
        if (!$nuovoruolo) {
43
            throw new \Exception('Inserire il nuvo ruolo');
44
        }
45
46 1
        $query = $this->em->createQueryBuilder()
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $query = $this->em->/** @scrutinizer ignore-call */ createQueryBuilder()
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47 1
                ->select('r')
48 1
                ->from('BiCoreBundle:Ruoli', 'r')
49 1
                ->where('r.ruolo = :ruolo')
50 1
                ->setParameter('ruolo', $ruoloesistente)
51 1
                ->getQuery()
52
        ;
53 1
        $ruoloesistenteobj = $query->getResult();
54 1
        if (!$ruoloesistenteobj) {
55
            throw new \Exception('Non esiste il ruolo '.$ruoloesistente);
0 ignored issues
show
Bug introduced by
Are you sure $ruoloesistente of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            throw new \Exception('Non esiste il ruolo './** @scrutinizer ignore-type */ $ruoloesistente);
Loading history...
56
        } else {
57 1
            $newruoloesistente = $ruoloesistenteobj[0];
58
        }
59
60 1
        $query = $this->em->createQueryBuilder()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61 1
                ->select('r')
62 1
                ->from('BiCoreBundle:Ruoli', 'r')
63 1
                ->where('r.ruolo = :ruolo')
64 1
                ->setParameter('ruolo', $nuovoruolo)
65 1
                ->getQuery()
66
        ;
67 1
        $nuovruoloobj = $query->getResult();
68 1
        if ($nuovruoloobj) {
69
            throw new \Exception('Esiste già il ruolo '.$nuovoruolo);
0 ignored issues
show
Bug introduced by
Are you sure $nuovoruolo of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            throw new \Exception('Esiste già il ruolo './** @scrutinizer ignore-type */ $nuovoruolo);
Loading history...
70
        }
71
72 1
        $output->writeln('<info>Inizio clonazione del ruolo '.$ruoloesistente.' in '.$nuovoruolo.'</info>');
73 1
        $newnuovoruolo = clone $newruoloesistente;
74 1
        $newnuovoruolo->setRuolo($nuovoruolo);
75 1
        $this->em->persist($newnuovoruolo);
76 1
        $this->em->flush();
77
78 1
        $query = $this->em->createQueryBuilder()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
79 1
                ->select('p')
80 1
                ->from('BiCoreBundle:Permessi', 'p')
81 1
                ->where('p.ruoli = :ruolo')
82 1
                ->setParameter('ruolo', $newruoloesistente)
83 1
                ->getQuery()
84
        ;
85 1
        $permessiobj = $query->getResult();
86 1
        foreach ($permessiobj as $permesso) {
87 1
            $newpermessi = clone $permesso;
88 1
            $newpermessi->setRuoli($newnuovoruolo);
89 1
            $this->em->persist($newpermessi);
90 1
            $this->em->flush();
91
        }
92 1
    }
93
}
94