Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

Command/GoogleAnalyticsOverviewsListCommand.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 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
16
{
17
    /** @var EntityManagerInterface */
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()
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
            if ($segmentId) {
77
                $overviews = $this->getOverviewsOfSegment($segmentId);
78
            } elseif ($configId) {
79
                $overviews = $this->getOverviewsOfConfig($configId);
80
            } else {
81
                $overviews = $this->getAllOverviews();
82
            }
83
84
            if (\count($overviews)) {
85
                $result = "\t".'<fg=green>' . \count($overviews) . '</fg=green> overviews found:';
86
                $output->writeln($result);
87
                foreach ($overviews as $overview) {
88
                    $result = "\t".'(id: <fg=cyan>' .$overview->getId() . '</fg=cyan>)';
89
                    $result .= "\t".'(config: <fg=cyan>' .$overview->getconfig()->getId() . '</fg=cyan>)';
90
                    if ($overview->getSegment()) {
91
                        $result .= "\t".'(segment: <fg=cyan>' .$overview->getSegment()->getId() . '</fg=cyan>)';
92
                    } else {
93
                        $result .= "\t\t";
94
                    }
95
                    $result .= "\t" . $overview->getTitle();
96
97
                    $output->writeln($result);
98
                }
99
            } else {
100
                $output->writeln('No overviews found');
101
            }
102
103
            return 0;
104
        } catch (\Exception $e) {
105
            $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
106
107
            return 1;
108
        }
109
    }
110
111
    /**
112
     * get all overviews of a segment
113
     *
114
     * @param int $segmentId
115
     *
116
     * @return array
117
     */
118 View Code Duplication
    private function getOverviewsOfSegment($segmentId)
119
    {
120
        // get specified segment
121
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
122
        $segment = $segmentRepository->find($segmentId);
123
124
        if (!$segment) {
125
            throw new \Exception('Unkown segment ID');
126
        }
127
128
        // get the overviews
129
        return $segment->getOverviews();
130
    }
131
132
    /**
133
     * get all overviews of a config
134
     *
135
     * @param int $configId
136
     *
137
     * @return array
138
     */
139 View Code Duplication
    private function getOverviewsOfConfig($configId)
140
    {
141
        // get specified config
142
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
143
        $config = $configRepository->find($configId);
144
145
        if (!$config) {
146
            throw new \Exception('Unkown config ID');
147
        }
148
149
        // get the overviews
150
        return $config->getOverviews();
151
    }
152
153
    /**
154
     * get all overviews
155
     *
156
     * @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...
157
     */
158
    private function getAllOverviews()
159
    {
160
        // get all overviews
161
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
162
163
        return $overviewRepository->findAll();
164
    }
165
}
166