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