These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\DashboardBundle\Command; |
||
4 | |||
5 | use Doctrine\ORM\EntityManagerInterface; |
||
6 | use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig; |
||
7 | use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview; |
||
8 | use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment; |
||
9 | use Kunstmaan\DashboardBundle\Repository\AnalyticsSegmentRepository; |
||
10 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
11 | use Symfony\Component\Console\Input\InputInterface; |
||
12 | use Symfony\Component\Console\Input\InputOption; |
||
13 | use Symfony\Component\Console\Output\OutputInterface; |
||
14 | |||
15 | /** |
||
16 | * @final since 5.1 |
||
17 | * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
||
18 | */ |
||
19 | class GoogleAnalyticsOverviewsGenerateCommand extends ContainerAwareCommand |
||
20 | { |
||
21 | /** @var EntityManagerInterface */ |
||
22 | private $em; |
||
23 | |||
24 | /** |
||
25 | * @param EntityManagerInterface|null $em |
||
26 | */ |
||
27 | public function __construct(/* EntityManagerInterface */ $em = null) |
||
28 | { |
||
29 | parent::__construct(); |
||
30 | |||
31 | if (!$em instanceof EntityManagerInterface) { |
||
32 | @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); |
||
33 | |||
34 | $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:overviews:generate' : $em); |
||
35 | |||
36 | return; |
||
37 | } |
||
38 | |||
39 | $this->em = $em; |
||
40 | } |
||
41 | |||
42 | View Code Duplication | protected function configure() |
|
43 | { |
||
44 | $this |
||
45 | ->setName('kuma:dashboard:widget:googleanalytics:overviews:generate') |
||
46 | ->setDescription('Generate overviews') |
||
47 | ->addOption( |
||
48 | 'config', |
||
49 | null, |
||
50 | InputOption::VALUE_OPTIONAL, |
||
51 | 'Specify to only update one config', |
||
52 | false |
||
53 | ) |
||
54 | ->addOption( |
||
55 | 'segment', |
||
56 | null, |
||
57 | InputOption::VALUE_OPTIONAL, |
||
58 | 'Specify to only update one segment', |
||
59 | false |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return int|void|null |
||
65 | */ |
||
66 | protected function execute(InputInterface $input, OutputInterface $output) |
||
67 | { |
||
68 | if (null === $this->em) { |
||
69 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
70 | } |
||
71 | |||
72 | // get params |
||
73 | $configId = false; |
||
74 | $segmentId = false; |
||
75 | |||
76 | try { |
||
77 | $configId = $input->getOption('config'); |
||
78 | $segmentId = $input->getOption('segment'); |
||
79 | } catch (\Exception $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
80 | } |
||
81 | |||
82 | try { |
||
83 | if ($segmentId) { |
||
84 | $this->generateOverviewsOfSegment($segmentId); |
||
85 | } elseif ($configId) { |
||
86 | $this->generateOverviewsOfConfig($configId); |
||
87 | } else { |
||
88 | $this->generateAllOverviews(); |
||
89 | } |
||
90 | |||
91 | $output->writeln('<fg=green>Overviews succesfully generated</fg=green>'); |
||
92 | |||
93 | return 0; |
||
94 | } catch (\InvalidArgumentException $e) { |
||
95 | $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>'); |
||
96 | |||
97 | return 1; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get all overviews of a segment |
||
103 | * |
||
104 | * @param int $segmentId |
||
105 | * |
||
106 | * @return array |
||
107 | * |
||
108 | * @throws \InvalidArgumentException |
||
109 | */ |
||
110 | View Code Duplication | private function generateOverviewsOfSegment($segmentId) |
|
111 | { |
||
112 | /** @var AnalyticsSegmentRepository $segmentRepository */ |
||
113 | $segmentRepository = $this->em->getRepository(AnalyticsSegment::class); |
||
114 | $segment = $segmentRepository->find($segmentId); |
||
115 | |||
116 | if (!$segment) { |
||
117 | throw new \InvalidArgumentException('Unknown segment ID'); |
||
118 | } |
||
119 | |||
120 | // init the segment |
||
121 | $segmentRepository->initSegment($segment); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get all overviews of a config |
||
126 | * |
||
127 | * @param int $configId |
||
128 | * |
||
129 | * @return array |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | private function generateOverviewsOfConfig($configId) |
||
134 | { |
||
135 | $configRepository = $this->em->getRepository(AnalyticsConfig::class); |
||
136 | $segmentRepository = $this->em->getRepository(AnalyticsSegment::class); |
||
137 | $overviewRepository = $this->em->getRepository(AnalyticsOverview::class); |
||
138 | // get specified config |
||
139 | $config = $configRepository->find($configId); |
||
140 | |||
141 | if (!$config) { |
||
142 | throw new \InvalidArgumentException('Unknown config ID'); |
||
143 | } |
||
144 | |||
145 | // create default overviews for this config if none exist yet |
||
146 | if (!\count($config->getOverviews())) { |
||
147 | $overviewRepository->addOverviews($config); |
||
148 | } |
||
149 | |||
150 | // init all the segments for this config |
||
151 | $segments = $config->getSegments(); |
||
152 | foreach ($segments as $segment) { |
||
153 | $segmentRepository->initSegment($segment); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * get all overviews |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | View Code Duplication | private function generateAllOverviews() |
|
163 | { |
||
164 | $configRepository = $this->em->getRepository(AnalyticsConfig::class); |
||
165 | $overviewRepository = $this->em->getRepository(AnalyticsOverview::class); |
||
166 | $segmentRepository = $this->em->getRepository(AnalyticsSegment::class); |
||
167 | $configs = $configRepository->findAll(); |
||
168 | |||
169 | foreach ($configs as $config) { |
||
170 | // add overviews if none exist yet |
||
171 | if (!\count($configRepository->findDefaultOverviews($config))) { |
||
172 | $overviewRepository->addOverviews($config); |
||
173 | } |
||
174 | |||
175 | // init all the segments for this config |
||
176 | $segments = $config->getSegments(); |
||
177 | foreach ($segments as $segment) { |
||
178 | $segmentRepository->initSegment($segment); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 |