Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Command/GoogleAnalyticsOverviewsListCommand.php (5 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
3
namespace Kunstmaan\DashboardBundle\Command;
4
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
7
use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Doctrine\ORM\EntityManagerInterface;
13
14
/**
15
 * @final since 5.1
16
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
17
 */
18
class GoogleAnalyticsOverviewsListCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /** @var EntityManagerInterface */
21
    private $em;
22
23
    /**
24
     * @param EntityManagerInterface|null $em
25
     */
26
    public function __construct(/* EntityManagerInterface */ $em = null)
27
    {
28
        parent::__construct();
29
30
        if (!$em instanceof EntityManagerInterface) {
31
            @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);
32
33
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:overviews:list' : $em);
34
35
            return;
36
        }
37
38
        $this->em = $em;
39
    }
40
41 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...
42
    {
43
        $this
44
            ->setName('kuma:dashboard:widget:googleanalytics:overviews:list')
45
            ->setDescription('List available overviews')
46
            ->addOption(
47
                'config',
48
                null,
49
                InputOption::VALUE_OPTIONAL,
50
                'Specify to only list overviews of one config',
51
                false
52
            )
53
            ->addOption(
54
                'segment',
55
                null,
56
                InputOption::VALUE_OPTIONAL,
57
                'Specify to only list overviews of one segment',
58
                false
59
            );
60
    }
61
62
    /**
63
     * @param InputInterface  $input
64
     * @param OutputInterface $output
65
     *
66
     * @return int|void|null
67
     */
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        if (null === $this->em) {
71
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
72
        }
73
74
        // get params
75
        $configId = $input->getOption('config');
76
        $segmentId = $input->getOption('segment');
77
78
        try {
79
            if ($segmentId) {
80
                $overviews = $this->getOverviewsOfSegment($segmentId);
81
            } elseif ($configId) {
82
                $overviews = $this->getOverviewsOfConfig($configId);
83
            } else {
84
                $overviews = $this->getAllOverviews();
85
            }
86
87
            if (\count($overviews)) {
88
                $result = "\t".'<fg=green>' . \count($overviews) . '</fg=green> overviews found:';
89
                $output->writeln($result);
90
                foreach ($overviews as $overview) {
91
                    $result = "\t".'(id: <fg=cyan>' .$overview->getId() . '</fg=cyan>)';
92
                    $result .= "\t".'(config: <fg=cyan>' .$overview->getconfig()->getId() . '</fg=cyan>)';
93
                    if ($overview->getSegment()) {
94
                        $result .= "\t".'(segment: <fg=cyan>' .$overview->getSegment()->getId() . '</fg=cyan>)';
95
                    } else {
96
                        $result .= "\t\t";
97
                    }
98
                    $result .= "\t" . $overview->getTitle();
99
100
                    $output->writeln($result);
101
                }
102
            } else {
103
                $output->writeln('No overviews found');
104
            }
105
106
            return 0;
107
        } catch (\Exception $e) {
108
            $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
109
110
            return 1;
111
        }
112
    }
113
114
    /**
115
     * get all overviews of a segment
116
     *
117
     * @param int $segmentId
118
     *
119
     * @return array
120
     */
121 View Code Duplication
    private function getOverviewsOfSegment($segmentId)
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...
122
    {
123
        // get specified segment
124
        $segmentRepository = $this->em->getRepository(AnalyticsSegment::class);
125
        $segment = $segmentRepository->find($segmentId);
126
127
        if (!$segment) {
128
            throw new \Exception('Unkown segment ID');
129
        }
130
131
        // get the overviews
132
        return $segment->getOverviews();
133
    }
134
135
    /**
136
     * get all overviews of a config
137
     *
138
     * @param int $configId
139
     *
140
     * @return array
141
     */
142 View Code Duplication
    private function getOverviewsOfConfig($configId)
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...
143
    {
144
        // get specified config
145
        $configRepository = $this->em->getRepository(AnalyticsConfig::class);
146
        $config = $configRepository->find($configId);
147
148
        if (!$config) {
149
            throw new \Exception('Unkown config ID');
150
        }
151
152
        // get the overviews
153
        return $config->getOverviews();
154
    }
155
156
    /**
157
     * get all overviews
158
     *
159
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use object[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
160
     */
161
    private function getAllOverviews()
162
    {
163
        // get all overviews
164
        $overviewRepository = $this->em->getRepository(AnalyticsOverview::class);
165
166
        return $overviewRepository->findAll();
167
    }
168
}
169