Completed
Pull Request — 6.0 (#1872)
by Sander
102:24 queued 60:56
created

GoogleAnalyticsOverviewsGenerateCommand.php (4 issues)

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
class GoogleAnalyticsOverviewsGenerateCommand extends ContainerAwareCommand
12
{
13
    /** @var EntityManagerInterface $em */
14
    private $em;
15
16 View Code Duplication
    protected function configure()
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...
17
    {
18
        $this
19
            ->setName('kuma:dashboard:widget:googleanalytics:overviews:generate')
20
            ->setDescription('Generate overviews')
21
            ->addOption(
22
                'config',
23
                null,
24
                InputOption::VALUE_OPTIONAL,
25
                'Specify to only update one config',
26
                false
27
            )
28
            ->addOption(
29
                'segment',
30
                null,
31
                InputOption::VALUE_OPTIONAL,
32
                'Specify to only update one segment',
33
                false
34
            );
35
    }
36
37
    /**
38
     * Inits instance variables for global usage.
39
     */
40
    private function init()
41
    {
42
        $this->em = $this->getContainer()->get('doctrine')->getManager();
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->init();
48
49
        // get params
50
        $configId  = false;
51
        $segmentId = false;
52
        try {
53
            $configId  = $input->getOption('config');
54
            $segmentId = $input->getOption('segment');
55
        } catch (\Exception $e) {
56
        }
57
58
        try {
59
            if ($segmentId) {
60
                $this->generateOverviewsOfSegment($segmentId);
61
            } else {
62
                if ($configId) {
63
                    $this->generateOverviewsOfConfig($configId);
64
                } else {
65
                    $this->generateAllOverviews();
66
                }
67
            }
68
69
            $output->writeln('<fg=green>Overviews succesfully generated</fg=green>');
70
        } catch (\InvalidArgumentException $e) {
71
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
72
        }
73
74
    }
75
76
77
    /**
78
     * Get all overviews of a segment
79
     *
80
     * @param int $segmentId
81
     *
82
     * @return array
0 ignored issues
show
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
83
     *
84
     * @throws \InvalidArgumentException
85
     */
86 View Code Duplication
    private function generateOverviewsOfSegment($segmentId)
87
    {
88
        /** @var AnalyticsSegmentRepository $segmentRepository */
89
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
90
        $segment           = $segmentRepository->find($segmentId);
91
92
        if (!$segment) {
93
            throw new \InvalidArgumentException('Unknown segment ID');
94
        }
95
96
        // init the segment
97
        $segmentRepository->initSegment($segment);
98
    }
99
100
    /**
101
     * Get all overviews of a config
102
     *
103
     * @param int $configId
104
     *
105
     * @return array
0 ignored issues
show
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
106
     *
107
     * @throws \InvalidArgumentException
108
     */
109
    private function generateOverviewsOfConfig($configId)
110
    {
111
        $configRepository   = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
112
        $segmentRepository  = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
113
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
114
        // get specified config
115
        $config = $configRepository->find($configId);
116
117
        if (!$config) {
118
            throw new \InvalidArgumentException('Unknown config ID');
119
        }
120
121
        // create default overviews for this config if none exist yet
122
        if (!count($config->getOverviews())) {
123
            $overviewRepository->addOverviews($config);
124
        }
125
126
        // init all the segments for this config
127
        $segments = $config->getSegments();
128
        foreach ($segments as $segment) {
129
            $segmentRepository->initSegment($segment);
130
        }
131
    }
132
133
    /**
134
     * get all overviews
135
     *
136
     * @return array
0 ignored issues
show
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
137
     */
138
    private function generateAllOverviews()
139
    {
140
        $configRepository   = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
141
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
142
        $segmentRepository  = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
143
        $configs            = $configRepository->findAll();
144
145
        foreach ($configs as $config) {
146
            // add overviews if none exist yet
147
            if (!count($configRepository->findDefaultOverviews($config))) {
148
                $overviewRepository->addOverviews($config);
149
            }
150
151
            // init all the segments for this config
152
            $segments = $config->getSegments();
153
            foreach ($segments as $segment) {
154
                $segmentRepository->initSegment($segment);
155
            }
156
        }
157
    }
158
}
159