Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

GoogleAnalyticsOverviewsGenerateCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

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\Repository\AnalyticsSegmentRepository;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @final since 5.1
14
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
15
 */
16
class GoogleAnalyticsOverviewsGenerateCommand extends ContainerAwareCommand
17
{
18
    /** @var EntityManagerInterface $em */
19
    private $em;
20
21
    /**
22
     * @param EntityManagerInterface|null $em
23
     */
24
    public function __construct(/* EntityManagerInterface */ $em = null)
25
    {
26
        parent::__construct();
27
28
        if (!$em instanceof EntityManagerInterface) {
29
            @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);
30
31
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:overviews:generate' : $em);
32
33
            return;
34
        }
35
36
        $this->em = $em;
37
    }
38
39 View Code Duplication
    protected function configure()
40
    {
41
        $this
42
            ->setName('kuma:dashboard:widget:googleanalytics:overviews:generate')
43
            ->setDescription('Generate overviews')
44
            ->addOption(
45
                'config',
46
                null,
47
                InputOption::VALUE_OPTIONAL,
48
                'Specify to only update one config',
49
                false
50
            )
51
            ->addOption(
52
                'segment',
53
                null,
54
                InputOption::VALUE_OPTIONAL,
55
                'Specify to only update one segment',
56
                false
57
            );
58
    }
59
60
    /**
61
     * @param InputInterface  $input
62
     * @param OutputInterface $output
63
     *
64
     * @return int|null|void
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
Consider adding a comment why this CATCH block is empty.
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
        } catch (\InvalidArgumentException $e) {
93
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
94
        }
95
    }
96
97
    /**
98
     * Get all overviews of a segment
99
     *
100
     * @param int $segmentId
101
     *
102
     * @return array
103
     *
104
     * @throws \InvalidArgumentException
105
     */
106 View Code Duplication
    private function generateOverviewsOfSegment($segmentId)
107
    {
108
        /** @var AnalyticsSegmentRepository $segmentRepository */
109
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
110
        $segment = $segmentRepository->find($segmentId);
111
112
        if (!$segment) {
113
            throw new \InvalidArgumentException('Unknown segment ID');
114
        }
115
116
        // init the segment
117
        $segmentRepository->initSegment($segment);
118
    }
119
120
    /**
121
     * Get all overviews of a config
122
     *
123
     * @param int $configId
124
     *
125
     * @return array
126
     *
127
     * @throws \InvalidArgumentException
128
     */
129
    private function generateOverviewsOfConfig($configId)
130
    {
131
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
132
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
133
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
134
        // get specified config
135
        $config = $configRepository->find($configId);
136
137
        if (!$config) {
138
            throw new \InvalidArgumentException('Unknown config ID');
139
        }
140
141
        // create default overviews for this config if none exist yet
142
        if (!\count($config->getOverviews())) {
143
            $overviewRepository->addOverviews($config);
144
        }
145
146
        // init all the segments for this config
147
        $segments = $config->getSegments();
148
        foreach ($segments as $segment) {
149
            $segmentRepository->initSegment($segment);
150
        }
151
    }
152
153
    /**
154
     * get all overviews
155
     *
156
     * @return array
157
     */
158 View Code Duplication
    private function generateAllOverviews()
159
    {
160
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
161
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
162
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
163
        $configs = $configRepository->findAll();
164
165
        foreach ($configs as $config) {
166
            // add overviews if none exist yet
167
            if (!\count($configRepository->findDefaultOverviews($config))) {
168
                $overviewRepository->addOverviews($config);
169
            }
170
171
            // init all the segments for this config
172
            $segments = $config->getSegments();
173
            foreach ($segments as $segment) {
174
                $segmentRepository->initSegment($segment);
175
            }
176
        }
177
    }
178
}
179