Completed
Push — master ( 9684f5...5d31f9 )
by Sander
36:56 queued 12:48
created

GoogleAnalyticsConfigsListCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\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 GoogleAnalyticsConfigsListCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    private $em;
20
21
    /**
22
     * @param EntityManagerInterface|null $em
23
     */
24
    public function __construct(/* EntityManagerInterface */ $em = null)
25
    {
26
        parent::__construct();
27
28
        if (!$em instanceof EntityManagerInterface) {
29
            @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
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
30
31
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:config:list' : $em);
32
33
            return;
34
        }
35
36
        $this->em = $em;
37
    }
38
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('kuma:dashboard:widget:googleanalytics:configs:list')
43
            ->setDescription('List available configs');
44
    }
45
46
    /**
47
     * @param InputInterface  $input
48
     * @param OutputInterface $output
49
     *
50
     * @return int|null|void
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        if (null === $this->em) {
55
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
56
        }
57
58
        $configs = $this->getconfigs();
59
60
        if (count($configs)) {
61
            $result = "\t".'<fg=green>' . count($configs) . '</fg=green> configs found:';
62
            $output->writeln($result);
63
            foreach ($configs as $config) {
64
                $result = "\t".'(id: <fg=cyan>' .$config->getId() . '</fg=cyan>)';
65
                $result .= "\t" . $config->getName();
66
67
                $output->writeln($result);
68
            }
69
        } else {
70
            $output->writeln('No configs found');
71
        }
72
    }
73
74
    /**
75
     * get all segments
76
     *
77
     * @return array
0 ignored issues
show
Documentation introduced by
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...
78
     */
79
    private function getconfigs()
80
    {
81
        // get all segments
82
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
83
84
        return $configRepository->findAll();
85
    }
86
}
87