Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Kunstmaan/AdminBundle/Command/ExceptionCommand.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\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\Exception;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @final since 5.1
14
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
15
 */
16
class ExceptionCommand 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...
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $em;
22
23
    /**
24
     * @param EntityManagerInterface|null $em
25
     */
26
    public function __construct(/* EntityManagerInterface */ $em = null)
27
    {
28
        parent::__construct();
29
30
        if (!$em instanceof EntityManagerInterface) {
31
            @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);
32
33
            $this->setName(null === $em ? 'kuma:exception:clear' : $em);
34
35
            return;
36
        }
37
38
        $this->em = $em;
39
    }
40
41
    protected function configure()
42
    {
43
        parent::configure();
44
45
        $this->setName('kuma:exception:clear')
46
            ->setDescription('Remove resolved exceptions based on days.')
47
            ->setDefinition(
48
                [
49
                    new InputArgument('days', InputArgument::OPTIONAL, 'Days', 7),
50
                ]
51
            );
52
    }
53
54
    /**
55
     * @param InputInterface  $input
56
     * @param OutputInterface $output
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        if (null === $this->em) {
61
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
62
        }
63
64
        $days = (int) $input->getArgument('days');
65
        if ($days <= 0) {
66
            $output->writeln('<bg=red;options=bold>Days number must be higher than 0</>');
67
        }
68
69
        $nowDate = new \DateTime();
70
        $convertDate = $nowDate->sub(
71
            new \DateInterval('P'.$days.'D')
72
        );
73
74
        $cp = 0;
75
        $exceptions = $this->em->getRepository(Exception::class)->findAllHigherThanDays($convertDate);
76
        if ($exceptions) {
77
            foreach ($exceptions as $exception) {
78
                $this->em->remove($exception);
79
                ++$cp;
80
            }
81
            $this->em->flush();
82
        }
83
84
        $output->writeln(sprintf('Removed exceptions <comment>%s</comment>', $cp));
85
    }
86
}
87