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 GoogleAnalyticsSegmentsListCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
/** @var EntityManagerInterface $em */ |
18
|
|
|
private $em; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param EntityManagerInterface|null $em |
22
|
|
|
*/ |
23
|
|
|
public function __construct(/* EntityManagerInterface */ $em = null) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
|
27
|
|
|
if (!$em instanceof EntityManagerInterface) { |
28
|
|
|
@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); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:segments:list' : $em); |
31
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->em = $em; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->setName('kuma:dashboard:widget:googleanalytics:segments:list') |
42
|
|
|
->setDescription('List available segments') |
43
|
|
|
->addOption( |
44
|
|
|
'config', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_OPTIONAL, |
47
|
|
|
'Specify to only list overviews of one config', |
48
|
|
|
false |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param InputInterface $input |
54
|
|
|
* @param OutputInterface $output |
55
|
|
|
* |
56
|
|
|
* @return int|null|void |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
if (null === $this->em) { |
61
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// get params |
65
|
|
|
$configId = $input->getOption('config'); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$segments = []; |
69
|
|
|
|
70
|
|
|
if ($configId) { |
71
|
|
|
$segments = $this->getSegmentsOfConfig($configId); |
72
|
|
|
} else { |
73
|
|
|
$segments = $this->getAllSegments(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (count($segments)) { |
77
|
|
|
$result = "\t".'<fg=green>' . count($segments) . '</fg=green> segments found:'; |
78
|
|
|
$output->writeln($result); |
79
|
|
|
foreach ($segments as $segment) { |
80
|
|
|
$result = "\t".'(id: <fg=cyan>' .$segment->getId() . '</fg=cyan>)'; |
81
|
|
|
$result .= "\t".'(config: <fg=cyan>' .$segment->getconfig()->getId() . '</fg=cyan>)'; |
82
|
|
|
$result .= "\t" .'<fg=cyan>'. $segment->getquery() .'</fg=cyan> ('.$segment->getName().')'; |
83
|
|
|
|
84
|
|
|
$output->writeln($result); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$output->writeln('No segments found'); |
88
|
|
|
} |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
$output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* get all segments of a config |
96
|
|
|
* |
97
|
|
|
* @param int $configId |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
private function getSegmentsOfConfig($configId) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// get specified config |
104
|
|
|
$configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
105
|
|
|
$config = $configRepository->find($configId); |
106
|
|
|
|
107
|
|
|
if (!$config) { |
108
|
|
|
throw new \Exception('Unkown config ID'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// get the segments |
112
|
|
|
return $config->getSegments(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* get all segments |
117
|
|
|
* |
118
|
|
|
* @return array |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
private function getAllSegments() |
121
|
|
|
{ |
122
|
|
|
// get all segments |
123
|
|
|
$segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
124
|
|
|
|
125
|
|
|
return $segmentRepository->findAll(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: