Issues (114)

Command/RemoveOldWebViewEmailsCommand.php (1 issue)

Severity
1
<?php
2
3
namespace Azine\EmailBundle\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
10
/**
11
 * Remove emails stored for web-view that are older than the configured time.
12
 *
13
 * @author dominik
14
 */
15
class RemoveOldWebViewEmailsCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated: since Symfony 4.2, use {@see Command} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

15
class RemoveOldWebViewEmailsCommand extends /** @scrutinizer ignore-deprecated */ ContainerAwareCommand
Loading history...
16
{
17
    /**
18
     * (non-PHPdoc).
19
     *
20
     * @see Symfony\Component\Console\Command.Command::configure()
21
     */
22 4
    protected function configure()
23
    {
24 4
        $this->setName('emails:remove-old-web-view-emails')
25 4
                ->setDescription('Remove all "SentEmail" from the database that are older than the configured time.')
26 4
                ->setDefinition(array(new InputArgument('keep', InputArgument::OPTIONAL, 'Remove all SentEmails older than "keep" days => also see azine_email_web_view_retention_time ')))
27 4
                ->setHelp(<<<EOF
28 4
The <info>emails:remove-old-web-view-emails</info> command deletes all SentEmail entities from the database
29
that are older than the number of days specified in the command-line parameter "keep" or configured in the
30
parameter "azine_email_web_view_retention" in your config.yml.
31
EOF
32
                )
33
        ;
34 4
    }
35
36
    /**
37
     * (non-PHPdoc).
38
     *
39
     * @see Symfony\Component\Console\Command.Command::execute()
40
     */
41 3
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        // get the number of days from the command-line-input
44 3
        $days = $input->getArgument('keep');
45
46
        // or if it is not given, from the config.yml
47 3
        if (!is_numeric($days)) {
48 2
            $days = $this->getContainer()->getParameter('azine_email_web_view_retention');
49 2
            $output->writeln("using the parameter from the configuration => '$days' days.");
50
        }
51
52 3
        if (null === $days) {
53 1
            throw new \Exception('either the commandline parameter "keep" or the "azine_email_web_view_retention" in your config.yml or the default-config has to be defined.');
54
        }
55
56
        // delete all SentEmails older than $date from the database
57 2
        $date = new \DateTime("$days days ago");
58 2
        $sentEmails = $this->getContainer()->get('doctrine')->getManager()->createQueryBuilder()
59 2
            ->delete('AzineEmailBundle:SentEmail', 's')
60 2
            ->where('s.sent < :sent')
61 2
            ->setParameter('sent', $date);
62 2
        $q = $sentEmails->getQuery();
63 2
        $result = $q->execute();
64
65 2
        $output->writeln($result.' SentEmails have been deleted that were older than '.$date->format('Y-m-d H:i:s'));
66 2
    }
67
}
68