Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Doctrine\ORM\EntityManagerInterface;
10
11
/**
12
 * @final since 5.1
13
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
14
 */
15
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...
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:list' : $em);
31
32
            return;
33
        }
34
35
        $this->em = $em;
36
    }
37
38 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...
39
    {
40
        $this
41
            ->setName('kuma:dashboard:widget:googleanalytics:overviews:list')
42
            ->setDescription('List available overviews')
43
            ->addOption(
44
                'config',
45
                null,
46
                InputOption::VALUE_OPTIONAL,
47
                'Specify to only list overviews of one config',
48
                false
49
            )
50
            ->addOption(
51
                'segment',
52
                null,
53
                InputOption::VALUE_OPTIONAL,
54
                'Specify to only list overviews of one segment',
55
                false
56
            );
57
    }
58
59
    /**
60
     * @param InputInterface  $input
61
     * @param OutputInterface $output
62
     *
63
     * @return int|null|void
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        if (null === $this->em) {
68
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
69
        }
70
71
        // get params
72
        $configId = $input->getOption('config');
73
        $segmentId = $input->getOption('segment');
74
75
        try {
76
            $overviews = [];
77
78
            if ($segmentId) {
79
                $overviews = $this->getOverviewsOfSegment($segmentId);
80
            } elseif ($configId) {
81
                $overviews = $this->getOverviewsOfConfig($configId);
82
            } else {
83
                $overviews = $this->getAllOverviews();
84
            }
85
86
            if (count($overviews)) {
87
                $result = "\t".'<fg=green>' . count($overviews) . '</fg=green> overviews found:';
88
                $output->writeln($result);
89
                foreach ($overviews as $overview) {
90
                    $result = "\t".'(id: <fg=cyan>' .$overview->getId() . '</fg=cyan>)';
91
                    $result .= "\t".'(config: <fg=cyan>' .$overview->getconfig()->getId() . '</fg=cyan>)';
92
                    if ($overview->getSegment()) {
93
                        $result .= "\t".'(segment: <fg=cyan>' .$overview->getSegment()->getId() . '</fg=cyan>)';
94
                    } else {
95
                        $result .= "\t\t";
96
                    }
97
                    $result .= "\t" . $overview->getTitle();
98
99
                    $output->writeln($result);
100
                }
101
            } else {
102
                $output->writeln('No overviews found');
103
            }
104
        } catch (\Exception $e) {
105
            $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
106
        }
107
    }
108
109
    /**
110
     * get all overviews of a segment
111
     *
112
     * @param int $segmentId
113
     *
114
     * @return array
115
     */
116 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...
117
    {
118
        // get specified segment
119
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
120
        $segment = $segmentRepository->find($segmentId);
121
122
        if (!$segment) {
123
            throw new \Exception('Unkown segment ID');
124
        }
125
126
        // get the overviews
127
        return $segment->getOverviews();
128
    }
129
130
    /**
131
     * get all overviews of a config
132
     *
133
     * @param int $configId
134
     *
135
     * @return array
136
     */
137 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...
138
    {
139
        // get specified config
140
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
141
        $config = $configRepository->find($configId);
142
143
        if (!$config) {
144
            throw new \Exception('Unkown config ID');
145
        }
146
147
        // get the overviews
148
        return $config->getOverviews();
149
    }
150
151
    /**
152
     * get all overviews
153
     *
154
     * @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...
155
     */
156
    private function getAllOverviews()
157
    {
158
        // get all overviews
159
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
160
161
        return $overviewRepository->findAll();
162
    }
163
}
164