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'); |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
56
|
|
|
} else { |
57
|
1 |
|
$newruoloesistente = $ruoloesistenteobj[0]; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$query = $this->em->createQueryBuilder() |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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
will produce issues in the first and second line, while this second example
will produce no issues.