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 Kunstmaan\DashboardBundle\Entity\AnalyticsConfig; |
||
6 | use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment; |
||
7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
8 | use Symfony\Component\Console\Input\InputInterface; |
||
9 | use Symfony\Component\Console\Input\InputOption; |
||
10 | use Symfony\Component\Console\Output\OutputInterface; |
||
11 | use Doctrine\ORM\EntityManagerInterface; |
||
12 | |||
13 | /** |
||
14 | * @final since 5.1 |
||
15 | * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
||
16 | */ |
||
17 | class GoogleAnalyticsSegmentsListCommand extends ContainerAwareCommand |
||
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); |
||
0 ignored issues
–
show
|
|||
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 | * @param InputInterface $input |
||
56 | * @param OutputInterface $output |
||
57 | * |
||
58 | * @return int|void|null |
||
59 | */ |
||
60 | protected function execute(InputInterface $input, OutputInterface $output) |
||
61 | { |
||
62 | if (null === $this->em) { |
||
63 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
64 | } |
||
65 | |||
66 | // get params |
||
67 | $configId = $input->getOption('config'); |
||
68 | |||
69 | try { |
||
70 | if ($configId) { |
||
71 | $segments = $this->getSegmentsOfConfig($configId); |
||
72 | } else { |
||
73 | $segments = $this->getAllSegments(); |
||
74 | } |
||
75 | |||
76 | if (\count($segments)) { |
||
77 | $result = "\t".'<fg=green>' . \count($segments) . '</fg=green> segments found:'; |
||
78 | $output->writeln($result); |
||
79 | foreach ($segments as $segment) { |
||
80 | $result = "\t".'(id: <fg=cyan>' .$segment->getId() . '</fg=cyan>)'; |
||
81 | $result .= "\t".'(config: <fg=cyan>' .$segment->getconfig()->getId() . '</fg=cyan>)'; |
||
82 | $result .= "\t" .'<fg=cyan>'. $segment->getquery() .'</fg=cyan> ('.$segment->getName().')'; |
||
83 | |||
84 | $output->writeln($result); |
||
85 | } |
||
86 | } else { |
||
87 | $output->writeln('No segments found'); |
||
88 | } |
||
89 | |||
90 | return 0; |
||
91 | } catch (\Exception $e) { |
||
92 | $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
||
93 | |||
94 | return 1; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * get all segments of a config |
||
100 | * |
||
101 | * @param int $configId |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | View Code Duplication | private function getSegmentsOfConfig($configId) |
|
106 | { |
||
107 | // get specified config |
||
108 | $configRepository = $this->em->getRepository(AnalyticsConfig::class); |
||
109 | $config = $configRepository->find($configId); |
||
110 | |||
111 | if (!$config) { |
||
112 | throw new \Exception('Unkown config ID'); |
||
113 | } |
||
114 | |||
115 | // get the segments |
||
116 | return $config->getSegments(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * get all segments |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | private function getAllSegments() |
||
125 | { |
||
126 | // get all segments |
||
127 | $segmentRepository = $this->em->getRepository(AnalyticsSegment::class); |
||
128 | |||
129 | return $segmentRepository->findAll(); |
||
130 | } |
||
131 | } |
||
132 |
If you suppress an error, we recommend checking for the error condition explicitly: