Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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 */
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) {
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('KunstmaanDashboardBundle:AnalyticsSegment');
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('KunstmaanDashboardBundle:AnalyticsConfig');
136
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
137
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
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()
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...
163
    {
164
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
165
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
166
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
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