Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
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 $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()
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
        } catch (\Exception $e) {
103
            $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
104
        }
105
    }
106
107
    /**
108
     * get all overviews of a segment
109
     *
110
     * @param int $segmentId
111
     *
112
     * @return array
113
     */
114 View Code Duplication
    private function getOverviewsOfSegment($segmentId)
115
    {
116
        // get specified segment
117
        $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
118
        $segment = $segmentRepository->find($segmentId);
119
120
        if (!$segment) {
121
            throw new \Exception('Unkown segment ID');
122
        }
123
124
        // get the overviews
125
        return $segment->getOverviews();
126
    }
127
128
    /**
129
     * get all overviews of a config
130
     *
131
     * @param int $configId
132
     *
133
     * @return array
134
     */
135 View Code Duplication
    private function getOverviewsOfConfig($configId)
136
    {
137
        // get specified config
138
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
139
        $config = $configRepository->find($configId);
140
141
        if (!$config) {
142
            throw new \Exception('Unkown config ID');
143
        }
144
145
        // get the overviews
146
        return $config->getOverviews();
147
    }
148
149
    /**
150
     * get all overviews
151
     *
152
     * @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...
153
     */
154
    private function getAllOverviews()
155
    {
156
        // get all overviews
157
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
158
159
        return $overviewRepository->findAll();
160
    }
161
}
162