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

Command/GoogleAnalyticsDataFlushCommand.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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
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 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);
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
     * @return int|void|null
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        if (null === $this->em) {
56
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
57
        }
58
59
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
60
61
        $configId = $input->getArgument('config') ? $input->getArgument('config') : false;
62
63
        try {
64
            $configRepository->flushConfig($configId);
65
            $output->writeln('<fg=green>Data flushed</fg=green>');
66
67
            return 0;
68
        } catch (\Exception $e) {
69
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
70
71
            return 1;
72
        }
73
    }
74
}
75