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

Command/GoogleAnalyticsConfigsListCommand.php (1 issue)

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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * @final since 5.1
13
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
14
 */
15
class GoogleAnalyticsConfigsListCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * @var EntityManagerInterface
19
     */
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
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...
31
32
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:config: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:configs:list')
44
            ->setDescription('List available configs');
45
    }
46
47
    /**
48
     * @return int|void|null
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        if (null === $this->em) {
53
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
54
        }
55
56
        $configs = $this->getconfigs();
57
58
        if (\count($configs)) {
59
            $result = "\t" . '<fg=green>' . \count($configs) . '</fg=green> configs found:';
60
            $output->writeln($result);
61
            foreach ($configs as $config) {
62
                $result = "\t" . '(id: <fg=cyan>' . $config->getId() . '</fg=cyan>)';
63
                $result .= "\t" . $config->getName();
64
65
                $output->writeln($result);
66
            }
67
        } else {
68
            $output->writeln('No configs found');
69
        }
70
71
        return 0;
72
    }
73
74
    /**
75
     * get all segments
76
     *
77
     * @return array
78
     */
79
    private function getconfigs()
80
    {
81
        // get all segments
82
        $configRepository = $this->em->getRepository(AnalyticsConfig::class);
83
84
        return $configRepository->findAll();
85
    }
86
}
87