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 | |||
10 | class GoogleAnalyticsSegmentsListCommand extends ContainerAwareCommand |
||
11 | { |
||
12 | /** @var EntityManager $em */ |
||
13 | private $em; |
||
14 | |||
15 | protected function configure() |
||
16 | { |
||
17 | $this |
||
18 | ->setName('kuma:dashboard:widget:googleanalytics:segments:list') |
||
19 | ->setDescription('List available segments') |
||
20 | ->addOption( |
||
21 | 'config', |
||
22 | null, |
||
23 | InputOption::VALUE_OPTIONAL, |
||
24 | 'Specify to only list overviews of one config', |
||
25 | false |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Inits instance variables for global usage. |
||
31 | */ |
||
32 | private function init() |
||
33 | { |
||
34 | $this->em = $this->getContainer()->get('doctrine')->getManager(); |
||
35 | } |
||
36 | |||
37 | protected function execute(InputInterface $input, OutputInterface $output) |
||
38 | { |
||
39 | $this->init(); |
||
40 | |||
41 | // get params |
||
42 | $configId = $input->getOption('config'); |
||
43 | |||
44 | try { |
||
45 | $segments = array(); |
||
46 | |||
47 | if ($configId) { |
||
48 | $segments = $this->getSegmentsOfConfig($configId); |
||
49 | } else { |
||
50 | $segments = $this->getAllSegments(); |
||
51 | } |
||
52 | |||
53 | if (count($segments)) { |
||
54 | $result = "\t".'<fg=green>' . count($segments) . '</fg=green> segments found:'; |
||
55 | $output->writeln($result); |
||
56 | foreach ($segments as $segment) { |
||
57 | $result = "\t".'(id: <fg=cyan>' .$segment->getId() . '</fg=cyan>)'; |
||
58 | $result .= "\t".'(config: <fg=cyan>' .$segment->getconfig()->getId() . '</fg=cyan>)'; |
||
59 | $result .= "\t" .'<fg=cyan>'. $segment->getquery() .'</fg=cyan> ('.$segment->getName().')'; |
||
60 | |||
61 | $output->writeln($result); |
||
62 | } |
||
63 | } else { |
||
64 | $output->writeln('No segments found'); |
||
65 | } |
||
66 | } catch (\Exception $e) { |
||
67 | $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>'); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * get all segments of a config |
||
73 | * |
||
74 | * @param int $configId |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | View Code Duplication | private function getSegmentsOfConfig($configId) |
|
0 ignored issues
–
show
|
|||
79 | { |
||
80 | // get specified config |
||
81 | $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig'); |
||
82 | $config = $configRepository->find($configId); |
||
83 | |||
84 | if (!$config) { |
||
85 | throw new \Exception('Unkown config ID'); |
||
86 | } |
||
87 | |||
88 | // get the segments |
||
89 | return $config->getSegments(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * get all segments |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | private function getAllSegments() |
||
98 | { |
||
99 | // get all segments |
||
100 | $segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment'); |
||
101 | |||
102 | return $segmentRepository->findAll(); |
||
103 | } |
||
104 | } |
||
105 |
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.