CronPruneLogsCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 31 4
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