Completed
Push — master ( 745336...aadd5d )
by Matthew
06:17
created

PruneCommand   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 4
dl 0
loc 135
ccs 69
cts 75
cp 0.92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C execute() 0 41 8
B pruneOldJobs() 0 32 6
C getInterval() 0 28 7
1
<?php
2
3
namespace Dtc\QueueBundle\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\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class PruneCommand extends ContainerAwareCommand
12
{
13
    const OLDER_MESSAGE = '<int>[d|m|y|h|i|s] Specify how old the jobs should (defaults to timestamp unless a quantifier is specified [d_ays, m_onths, y_years, h_ours, i_minutes, s_econds';
14
15 4
    protected function configure()
16
    {
17
        $this
18 4
        ->setName('dtc:queue:prune')
19 4
        ->setDescription('Prune job with error status')
20 4
        ->addArgument('type', InputArgument::REQUIRED, '<stalled|error|expired|old|old_runs> Prune stalled, erroneous, expired, or old jobs')
21 4
            ->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE);
22 4
    }
23
24 4
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26 4
        $container = $this->getContainer();
27 4
        $jobManager = $container->get('dtc_queue.job_manager');
28 4
        $type = $input->getArgument('type');
29
        switch ($type) {
30 4
            case 'error':
31 1
                $count = $jobManager->pruneErroneousJobs();
32 1
                $output->writeln("$count Erroneous Job(s) pruned");
33 1
                break;
34 3
            case 'expired':
35 1
                $count = $jobManager->pruneExpiredJobs();
36 1
                $output->writeln("$count Expired Job(s) pruned");
37 1
                break;
38 2
            case 'stalled':
39 1
                $count = $jobManager->pruneStalledJobs();
40 1
                $output->writeln("$count Stalled Job(s) pruned");
41 1
                break;
42 1
            case 'old':
43 1
            case 'old_runs':
44 1
                $older = $input->getOption('older');
45 1
                if (!$older) {
46 1
                    $output->writeln('<error>--older must be specified</error>');
47
48 1
                    return 1;
49
                }
50 1
                if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}$/", $older, $matches)) {
51 1
                    $output->writeln('<error>Wrong format for --older</error>');
52
53 1
                    return 1;
54
                }
55
56 1
                return $this->pruneOldJobs($matches, $type, $output);
57
            default:
58
                $output->writeln("<error>Unknown type $type.</error>");
59
60
                return 1;
61
        }
62
63 3
        return 0;
64
    }
65
66
    /**
67
     * @param string[]        $matches
68
     * @param OutputInterface $output
69
     *
70
     * @return int
71
     *
72
     * @throws \Exception
73
     */
74 1
    protected function pruneOldJobs(array $matches, $type, OutputInterface $output)
75
    {
76 1
        $durationOrTimestamp = intval($matches[1]);
77 1
        $modifier = isset($matches[2]) ? $matches[2] : null;
78
79 1
        if (!$durationOrTimestamp) {
80
            $output->writeln('<error>No duration or timestamp passed in.</error>');
81
82
            return 1;
83
        }
84 1
        $olderThan = new \DateTime();
85 1
        if (null === $modifier) {
86 1
            $olderThan->setTimestamp($durationOrTimestamp);
87
        } else {
88 1
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
89 1
            $olderThan->sub($interval);
90
        }
91 1
        $container = $this->getContainer();
92
        switch ($type) {
93 1
            case 'old':
94 1
                $count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan);
95 1
                break;
96 1
            case 'old_runs':
97 1
                $count = $container->get('dtc_queue.run_manager')->pruneArchivedRuns($olderThan);
98 1
                break;
99
            default:
100
                throw new \Exception("Unknown type $type");
101
        }
102 1
        $output->writeln("$count Archived Job(s) pruned");
103
104 1
        return 0;
105
    }
106
107
    /**
108
     * Returns the date interval based on the modifier and the duration.
109
     *
110
     * @param string $modifier
111
     * @param int    $duration
112
     *
113
     * @return \DateInterval
114
     *
115
     * @throws \Exception
116
     */
117 1
    protected function getInterval($modifier, $duration)
118
    {
119
        switch ($modifier) {
120 1
            case 'd':
121 1
                $interval = new \DateInterval("P${duration}D");
122 1
                break;
123 1
            case 'm':
124 1
                $interval = new \DateInterval("P${duration}M");
125
                break;
126
            case 'y':
127
                $interval = new \DateInterval("P${duration}Y");
128 1
                break;
129 1
            case 'h':
130 1
                $interval = new \DateInterval("PT${duration}H");
131 1
                break;
132 1
            case 'i':
133 1
                $seconds = $duration * 60;
134 1
                $interval = new \DateInterval("PT${seconds}S");
135 1
                break;
136 1
            case 's':
137 1
                $interval = new \DateInterval("PT${duration}S");
138 1
                break;
139
            default:
140
                throw new \Exception("Unknown duration modifier: $modifier");
141
        }
142
143 1
        return $interval;
144
    }
145
}
146