GoogleAnalyticsDataFlushCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Kunstmaan\DashboardBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Doctrine\ORM\EntityManagerInterface;
10
11
/**
12
 * @final since 5.1
13
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
14
 */
15
class GoogleAnalyticsDataFlushCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /** @var EntityManagerInterface */
18
    private $em;
19
20
    /**
21
     * @param EntityManagerInterface|null $em
22
     */
23
    public function __construct(/* EntityManagerInterface */ $em = null)
24
    {
25
        parent::__construct();
26
27
        if (!$em instanceof EntityManagerInterface) {
28
            @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...
29
30
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:data:flush' : $em);
31
32
            return;
33
        }
34
35
        $this->em = $em;
36
    }
37
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('kuma:dashboard:widget:googleanalytics:data:flush')
42
            ->setDescription('Flush the data of a config')
43
            ->addArgument(
44
                'config',
45
                InputArgument::OPTIONAL,
46
                'The config id'
47
            );
48
    }
49
50
    /**
51
     * @param InputInterface  $input
52
     * @param OutputInterface $output
53
     *
54
     * @return int|void|null
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
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
63
64
        $configId = $input->getArgument('config') ? $input->getArgument('config') : false;
65
66
        try {
67
            $configRepository->flushConfig($configId);
68
            $output->writeln('<fg=green>Data flushed</fg=green>');
69
70
            return 0;
71
        } catch (\Exception $e) {
72
            $output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
73
74
            return 1;
75
        }
76
    }
77
}
78