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\AnalyticsSegment; |
||
7 | use Kunstmaan\DashboardBundle\Repository\AnalyticsSegmentRepository; |
||
8 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
9 | use Symfony\Component\Console\Input\InputInterface; |
||
10 | use Symfony\Component\Console\Input\InputOption; |
||
11 | use Symfony\Component\Console\Output\OutputInterface; |
||
12 | |||
13 | /** |
||
14 | * Class GoogleAnalyticsOverviewsGenerateCommand |
||
15 | */ |
||
16 | class GoogleAnalyticsOverviewsGenerateCommand extends ContainerAwareCommand |
||
17 | { |
||
18 | /** |
||
19 | * @var EntityManagerInterface |
||
20 | */ |
||
21 | private $em; |
||
22 | |||
23 | View Code Duplication | protected function configure() |
|
0 ignored issues
–
show
|
|||
24 | { |
||
25 | $this |
||
26 | ->setName('kuma:dashboard:widget:googleanalytics:overviews:generate') |
||
27 | ->setDescription('Generate overviews') |
||
28 | ->addOption( |
||
29 | 'config', |
||
30 | null, |
||
31 | InputOption::VALUE_OPTIONAL, |
||
32 | 'Specify to only update one config', |
||
33 | false |
||
34 | ) |
||
35 | ->addOption( |
||
36 | 'segment', |
||
37 | null, |
||
38 | InputOption::VALUE_OPTIONAL, |
||
39 | 'Specify to only update one segment', |
||
40 | false |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Inits instance variables for global usage. |
||
46 | */ |
||
47 | private function init() |
||
48 | { |
||
49 | $this->em = $this->getContainer()->get('doctrine')->getManager(); |
||
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 | $this->init(); |
||
61 | |||
62 | // get params |
||
63 | $configId = false; |
||
64 | $segmentId = false; |
||
65 | try { |
||
66 | $configId = $input->getOption('config'); |
||
67 | $segmentId = $input->getOption('segment'); |
||
68 | } catch (\Exception $e) { |
||
69 | } |
||
70 | |||
71 | try { |
||
72 | if ($segmentId) { |
||
73 | $this->generateOverviewsOfSegment($segmentId); |
||
74 | } else { |
||
75 | if ($configId) { |
||
76 | $this->generateOverviewsOfConfig($configId); |
||
77 | } else { |
||
78 | $this->generateAllOverviews(); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $output->writeln('<fg=green>Overviews succesfully generated</fg=green>'); |
||
83 | } catch (\InvalidArgumentException $e) { |
||
84 | $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get all overviews of a segment |
||
90 | * |
||
91 | * @param int $segmentId |
||
92 | * |
||
93 | * @throws \InvalidArgumentException |
||
94 | */ |
||
95 | View Code Duplication | private function generateOverviewsOfSegment($segmentId) |
|
96 | { |
||
97 | /** @var AnalyticsSegmentRepository $segmentRepository */ |
||
98 | $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
||
99 | /** @var AnalyticsSegment $segment */ |
||
100 | $segment = $segmentRepository->find($segmentId); |
||
101 | |||
102 | if (!$segment) { |
||
103 | throw new \InvalidArgumentException('Unknown segment ID'); |
||
104 | } |
||
105 | |||
106 | // init the segment |
||
107 | $segmentRepository->initSegment($segment); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get all overviews of a config |
||
112 | * |
||
113 | * @param int $configId |
||
114 | * |
||
115 | * @throws \InvalidArgumentException |
||
116 | */ |
||
117 | private function generateOverviewsOfConfig($configId) |
||
118 | { |
||
119 | $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
||
120 | $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
||
121 | $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview'); |
||
122 | // get specified config |
||
123 | $config = $configRepository->find($configId); |
||
124 | |||
125 | if (!$config) { |
||
126 | throw new \InvalidArgumentException('Unknown config ID'); |
||
127 | } |
||
128 | |||
129 | // create default overviews for this config if none exist yet |
||
130 | if (!\count($config->getOverviews())) { |
||
131 | $overviewRepository->addOverviews($config); |
||
132 | } |
||
133 | |||
134 | // init all the segments for this config |
||
135 | foreach ($config->getSegments() as $segment) { |
||
136 | $segmentRepository->initSegment($segment); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get all overviews |
||
142 | */ |
||
143 | View Code Duplication | private function generateAllOverviews() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
144 | { |
||
145 | $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
||
146 | $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview'); |
||
147 | $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
||
148 | $configs = $configRepository->findAll(); |
||
149 | |||
150 | foreach ($configs as $config) { |
||
151 | // add overviews if none exist yet |
||
152 | if (!\count($configRepository->findDefaultOverviews($config))) { |
||
153 | $overviewRepository->addOverviews($config); |
||
154 | } |
||
155 | |||
156 | // init all the segments for this config |
||
157 | $segments = $config->getSegments(); |
||
158 | foreach ($segments as $segment) { |
||
159 | $segmentRepository->initSegment($segment); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.