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 GoogleAnalyticsSegmentsListCommand 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:segments:list' : $em); |
||
30 | |||
31 | return; |
||
32 | } |
||
33 | |||
34 | $this->em = $em; |
||
35 | } |
||
36 | |||
37 | protected function configure() |
||
38 | { |
||
39 | $this |
||
40 | ->setName('kuma:dashboard:widget:googleanalytics:segments:list') |
||
41 | ->setDescription('List available segments') |
||
42 | ->addOption( |
||
43 | 'config', |
||
44 | null, |
||
45 | InputOption::VALUE_OPTIONAL, |
||
46 | 'Specify to only list overviews of one config', |
||
47 | false |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param InputInterface $input |
||
53 | * @param OutputInterface $output |
||
54 | * @return int|null|void |
||
55 | */ |
||
56 | protected function execute(InputInterface $input, OutputInterface $output) |
||
57 | { |
||
58 | if (null === $this->em) { |
||
59 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
60 | } |
||
61 | |||
62 | // get params |
||
63 | $configId = $input->getOption('config'); |
||
64 | |||
65 | try { |
||
66 | $segments = []; |
||
67 | |||
68 | if ($configId) { |
||
69 | $segments = $this->getSegmentsOfConfig($configId); |
||
70 | } else { |
||
71 | $segments = $this->getAllSegments(); |
||
72 | } |
||
73 | |||
74 | if (count($segments)) { |
||
75 | $result = "\t".'<fg=green>' . count($segments) . '</fg=green> segments found:'; |
||
76 | $output->writeln($result); |
||
77 | foreach($segments as $segment) { |
||
78 | $result = "\t".'(id: <fg=cyan>' .$segment->getId() . '</fg=cyan>)'; |
||
79 | $result .= "\t".'(config: <fg=cyan>' .$segment->getconfig()->getId() . '</fg=cyan>)'; |
||
80 | $result .= "\t" .'<fg=cyan>'. $segment->getquery() .'</fg=cyan> ('.$segment->getName().')'; |
||
81 | |||
82 | $output->writeln($result); |
||
83 | } |
||
84 | } else { |
||
85 | $output->writeln('No segments found'); |
||
86 | } |
||
87 | } catch (\Exception $e) { |
||
88 | $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
||
89 | } |
||
90 | |||
91 | } |
||
92 | |||
93 | /** |
||
94 | * get all segments of a config |
||
95 | * @param int $configId |
||
96 | * @return array |
||
97 | */ |
||
98 | View Code Duplication | private function getSegmentsOfConfig($configId) |
|
0 ignored issues
–
show
|
|||
99 | { |
||
100 | // get specified config |
||
101 | $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
||
102 | $config = $configRepository->find($configId); |
||
103 | |||
104 | if (!$config) { |
||
105 | throw new \Exception('Unkown config ID'); |
||
106 | } |
||
107 | |||
108 | // get the segments |
||
109 | return $config->getSegments(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * get all segments |
||
114 | * |
||
115 | * @return array |
||
0 ignored issues
–
show
|
|||
116 | */ |
||
117 | private function getAllSegments() |
||
118 | { |
||
119 | // get all segments |
||
120 | $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
||
121 | return $segmentRepository->findAll(); |
||
122 | } |
||
123 | } |
||
124 |
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.