Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Command/GoogleAnalyticsSegmentsListCommand.php (3 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 Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
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
13
/**
14
 * @final since 5.1
15
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
16
 */
17
class GoogleAnalyticsSegmentsListCommand 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...
18
{
19
    /** @var EntityManagerInterface */
20
    private $em;
21
22
    /**
23
     * @param EntityManagerInterface|null $em
24
     */
25
    public function __construct(/* EntityManagerInterface */ $em = null)
26
    {
27
        parent::__construct();
28
29
        if (!$em instanceof EntityManagerInterface) {
30
            @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);
31
32
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:segments:list' : $em);
33
34
            return;
35
        }
36
37
        $this->em = $em;
38
    }
39
40
    protected function configure()
41
    {
42
        $this
43
            ->setName('kuma:dashboard:widget:googleanalytics:segments:list')
44
            ->setDescription('List available segments')
45
            ->addOption(
46
                'config',
47
                null,
48
                InputOption::VALUE_OPTIONAL,
49
                'Specify to only list overviews of one config',
50
                false
51
            );
52
    }
53
54
    /**
55
     * @return int|void|null
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        if (null === $this->em) {
60
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
61
        }
62
63
        // get params
64
        $configId = $input->getOption('config');
65
66
        try {
67
            if ($configId) {
68
                $segments = $this->getSegmentsOfConfig($configId);
69
            } else {
70
                $segments = $this->getAllSegments();
71
            }
72
73
            if (\count($segments)) {
74
                $result = "\t" . '<fg=green>' . \count($segments) . '</fg=green> segments found:';
75
                $output->writeln($result);
76
                foreach ($segments as $segment) {
77
                    $result = "\t" . '(id: <fg=cyan>' . $segment->getId() . '</fg=cyan>)';
78
                    $result .= "\t" . '(config: <fg=cyan>' . $segment->getconfig()->getId() . '</fg=cyan>)';
79
                    $result .= "\t" . '<fg=cyan>' . $segment->getquery() . '</fg=cyan> (' . $segment->getName() . ')';
80
81
                    $output->writeln($result);
82
                }
83
            } else {
84
                $output->writeln('No segments found');
85
            }
86
87
            return 0;
88
        } catch (\Exception $e) {
89
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
90
91
            return 1;
92
        }
93
    }
94
95
    /**
96
     * get all segments of a config
97
     *
98
     * @param int $configId
99
     *
100
     * @return array
101
     */
102 View Code Duplication
    private function getSegmentsOfConfig($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...
103
    {
104
        // get specified config
105
        $configRepository = $this->em->getRepository(AnalyticsConfig::class);
106
        $config = $configRepository->find($configId);
107
108
        if (!$config) {
109
            throw new \Exception('Unkown config ID');
110
        }
111
112
        // get the segments
113
        return $config->getSegments();
114
    }
115
116
    /**
117
     * get all segments
118
     *
119
     * @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...
120
     */
121
    private function getAllSegments()
122
    {
123
        // get all segments
124
        $segmentRepository = $this->em->getRepository(AnalyticsSegment::class);
125
126
        return $segmentRepository->findAll();
127
    }
128
}
129