CronPruneLogsCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Alpixel\Bundle\CronBundle\Command;
4
5
use Alpixel\Bundle\CronBundle\Entity\CronJobResult;
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
class CronPruneLogsCommand extends ContainerAwareCommand
12
{
13
    protected function configure()
14
    {
15
        $this->setName('cron:pruneLogs')
16
             ->setDescription('Prunes the logs for each cron job, leaving only recent failures and the most recent success')
17
             ->addArgument('job', InputArgument::OPTIONAL, 'Operate only on this job');
18
    }
19
20
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
23
        $job = $input->getArgument('job');
24
25
        if ($job) {
26
            $output->writeln("Pruning logs for cron job $job");
27
        } else {
28
            $output->writeln('Pruning logs for all cron jobs');
29
        }
30
31
        if ($job) {
32
            $jobObj = $em->getRepository('CronBundle:CronJob')->findOneByCommand($job);
33
            if (!$jobObj) {
34
                $output->writeln("Couldn't find a job by the name of ".$job);
35
36
                return CronJobResult::FAILED;
37
            }
38
39
            $em->getRepository('CronBundle:CronJobResult')->deleteOldLogs($jobObj);
40
        } else {
41
            $em->getRepository('CronBundle:CronJobResult')->deleteOldLogs();
42
        }
43
44
        // Flush the EM
45
        $em->flush();
46
47
        $output->writeln('Logs pruned successfully');
48
49
        return CronJobResult::SUCCEEDED;
50
    }
51
}
52