1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\DashboardBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @final since 5.1 |
13
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
14
|
|
|
*/ |
15
|
|
|
class GoogleAnalyticsConfigFlushCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EntityManagerInterface |
19
|
|
|
*/ |
20
|
|
|
private $em; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param EntityManagerInterface|null $em |
24
|
|
|
*/ |
25
|
|
|
public function __construct(/* EntityManagerInterface */ $em = null) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
if (!$em instanceof EntityManagerInterface) { |
30
|
|
|
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:config:flush' : $em); |
33
|
|
|
|
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->em = $em; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
|
|
->setName('kuma:dashboard:widget:googleanalytics:config:flush') |
44
|
|
|
->setDescription('Flush configs') |
45
|
|
|
->addOption( |
46
|
|
|
'config', |
47
|
|
|
null, |
48
|
|
|
InputOption::VALUE_OPTIONAL, |
49
|
|
|
'Specify to only flush one config', |
50
|
|
|
false |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param InputInterface $input |
56
|
|
|
* @param OutputInterface $output |
57
|
|
|
* |
58
|
|
|
* @return int|void|null |
59
|
|
|
*/ |
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
if (null === $this->em) { |
63
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
67
|
|
|
$configId = $input->getOption('config'); |
68
|
|
|
$configs = []; |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
if ($configId) { |
72
|
|
|
$configs[] = $configRepository->find($configId); |
73
|
|
|
} else { |
74
|
|
|
$configs = $configRepository->findAll(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($configs as $config) { |
78
|
|
|
$this->em->remove($config); |
79
|
|
|
} |
80
|
|
|
$this->em->flush(); |
81
|
|
|
$output->writeln('<fg=green>Config flushed</fg=green>'); |
82
|
|
|
|
83
|
|
|
return 0; |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
$output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
86
|
|
|
|
87
|
|
|
return 1; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: