Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
namespace Kunstmaan\DashboardBundle\Command;
3
4
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Doctrine\ORM\EntityManagerInterface;
9
10
/**
11
 * @final since 5.1
12
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
13
 */
14
class GoogleAnalyticsOverviewsListCommand extends ContainerAwareCommand
15
{
16
    /** @var EntityManagerInterface $em */
17
    private $em;
18
19
    /**
20
     * @param EntityManagerInterface|null   $em
21
     */
22
    public function __construct(/* EntityManagerInterface */ $em = null)
23
    {
24
        parent::__construct();
25
26
        if (!$em instanceof EntityManagerInterface) {
27
            @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);
28
29
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:overviews:list' : $em);
30
31
            return;
32
        }
33
34
        $this->em = $em;
35
    }
36
37 View Code Duplication
    protected function configure()
38
    {
39
        $this
40
            ->setName('kuma:dashboard:widget:googleanalytics:overviews:list')
41
            ->setDescription('List available overviews')
42
            ->addOption(
43
                'config',
44
                null,
45
                InputOption::VALUE_OPTIONAL,
46
                'Specify to only list overviews of one config',
47
                false
48
            )
49
            ->addOption(
50
                'segment',
51
                null,
52
                InputOption::VALUE_OPTIONAL,
53
                'Specify to only list overviews of one segment',
54
                false
55
            );
56
    }
57
58
    /**
59
     * @param InputInterface  $input
60
     * @param OutputInterface $output
61
     * @return int|null|void
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        if (null === $this->em) {
66
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
67
        }
68
69
        // get params
70
        $configId  = $input->getOption('config');
71
        $segmentId = $input->getOption('segment');
72
73
        try {
74
            $overviews = [];
75
76 View Code Duplication
            if ($segmentId) {
77
                $overviews = $this->getOverviewsOfSegment($segmentId);
78
            } else if ($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
109
    /**
110
     * get all overviews of a segment
111
     * @param int $segmentId
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
     * @param int $configId
131
     * @return array
132
     */
133 View Code Duplication
    private function getOverviewsOfConfig($configId)
134
    {
135
        // get specified config
136
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
137
        $config = $configRepository->find($configId);
138
139
        if (!$config) {
140
            throw new \Exception('Unkown config ID');
141
        }
142
143
        // get the overviews
144
        return $config->getOverviews();
145
    }
146
147
    /**
148
     * get all overviews
149
     *
150
     * @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...
151
     */
152
    private function getAllOverviews()
153
    {
154
        // get all overviews
155
        $overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
156
        return $overviewRepository->findAll();
157
    }
158
}
159