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

Command/GoogleAnalyticsConfigFlushCommand.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\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
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 GoogleAnalyticsConfigFlushCommand 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
    /**
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);
31
32
            $this->setName(null === $em ? 'kuma:dashboard:widget:googleanalytics:config:flush' : $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:config:flush')
44
            ->setDescription('Flush configs')
45
            ->addOption(
46
                'config',
47
                null,
48
                InputOption::VALUE_OPTIONAL,
49
                'Specify to only flush one config',
50
                false
51
            );
52
    }
53
54
    /**
55
     * @return int|void|null
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        if (null === $this->em) {
60
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
61
        }
62
63
        $configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
64
        $configId = $input->getOption('config');
65
        $configs = [];
66
67
        try {
68
            if ($configId) {
69
                $configs[] = $configRepository->find($configId);
70
            } else {
71
                $configs = $configRepository->findAll();
72
            }
73
74
            foreach ($configs as $config) {
75
                $this->em->remove($config);
76
            }
77
            $this->em->flush();
78
            $output->writeln('<fg=green>Config flushed</fg=green>');
79
80
            return 0;
81
        } catch (\Exception $e) {
82
            $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
83
84
            return 1;
85
        }
86
    }
87
}
88