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\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
     * @param InputInterface  $input
65
     * @param OutputInterface $output
66
     *
67
     * @return int|null|void
68
     */
69
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71
        if (null === $this->em) {
72
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
73
        }
74
75
        // get params
76
        $configId = false;
77
        $segmentId = false;
78
79
        try {
80
            $configId = $input->getOption('config');
81
            $segmentId = $input->getOption('segment');
82
        } catch (\Exception $e) {
83
        }
84
85
        try {
86
            if ($segmentId) {
87
                $this->generateOverviewsOfSegment($segmentId);
88
            } elseif ($configId) {
89
                $this->generateOverviewsOfConfig($configId);
90
            } else {
91
                $this->generateAllOverviews();
92
            }
93
94
            $output->writeln('<fg=green>Overviews succesfully generated</fg=green>');
95
96
            return 0;
97
        } catch (\InvalidArgumentException $e) {
98
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
99
100
            return 1;
101
        }
102
    }
103
104
    /**
105
     * Get all overviews of a segment
106
     *
107
     * @param int $segmentId
108
     *
109
     * @return array
110
     *
111
     * @throws \InvalidArgumentException
112
     */
113 View Code Duplication
    private function generateOverviewsOfSegment($segmentId)
114
    {
115
        /** @var AnalyticsSegmentRepository $segmentRepository */
116
        $segmentRepository = $this->em->getRepository(AnalyticsSegment::class);
117
        $segment = $segmentRepository->find($segmentId);
118
119
        if (!$segment) {
120
            throw new \InvalidArgumentException('Unknown segment ID');
121
        }
122
123
        // init the segment
124
        $segmentRepository->initSegment($segment);
125
    }
126
127
    /**
128
     * Get all overviews of a config
129
     *
130
     * @param int $configId
131
     *
132
     * @return array
133
     *
134
     * @throws \InvalidArgumentException
135
     */
136
    private function generateOverviewsOfConfig($configId)
137
    {
138
        $configRepository = $this->em->getRepository(AnalyticsConfig::class);
139
        $segmentRepository = $this->em->getRepository(AnalyticsSegment::class);
140
        $overviewRepository = $this->em->getRepository(AnalyticsOverview::class);
141
        // get specified config
142
        $config = $configRepository->find($configId);
143
144
        if (!$config) {
145
            throw new \InvalidArgumentException('Unknown config ID');
146
        }
147
148
        // create default overviews for this config if none exist yet
149
        if (!\count($config->getOverviews())) {
150
            $overviewRepository->addOverviews($config);
151
        }
152
153
        // init all the segments for this config
154
        $segments = $config->getSegments();
155
        foreach ($segments as $segment) {
156
            $segmentRepository->initSegment($segment);
157
        }
158
    }
159
160
    /**
161
     * get all overviews
162
     *
163
     * @return array
164
     */
165 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...
166
    {
167
        $configRepository = $this->em->getRepository(AnalyticsConfig::class);
168
        $overviewRepository = $this->em->getRepository(AnalyticsOverview::class);
169
        $segmentRepository = $this->em->getRepository(AnalyticsSegment::class);
170
        $configs = $configRepository->findAll();
171
172
        foreach ($configs as $config) {
173
            // add overviews if none exist yet
174
            if (!\count($configRepository->findDefaultOverviews($config))) {
175
                $overviewRepository->addOverviews($config);
176
            }
177
178
            // init all the segments for this config
179
            $segments = $config->getSegments();
180
            foreach ($segments as $segment) {
181
                $segmentRepository->initSegment($segment);
182
            }
183
        }
184
    }
185
}
186