Completed
Push — master ( 1204eb...5574d8 )
by Matthew
05:57
created

PruneCommand::getIntervalTime()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 9.2
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
crap 4.0092
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 5
    protected function configure()
16
    {
17
        $this
18 5
        ->setName('dtc:queue:prune')
19 5
        ->setDescription('Prune job with error status')
20 5
        ->addArgument('type', InputArgument::REQUIRED, '<stalled|stalled_runs|error|expired|old|old_runs|old_job_timings> Prune stalled, erroneous, expired, or old jobs')
21 5
            ->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE);
22 5
    }
23
24 5 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26 5
        $container = $this->getContainer();
27 5
        $jobManager = $container->get('dtc_queue.job_manager');
28 5
        $type = $input->getArgument('type');
29
        switch ($type) {
30 5
            case 'error':
31 1
                $count = $jobManager->pruneErroneousJobs();
32 1
                $output->writeln("$count Erroneous Job(s) pruned");
33 1
                break;
34 4
            case 'expired':
35 1
                $count = $jobManager->pruneExpiredJobs();
36 1
                $output->writeln("$count Expired Job(s) pruned");
37 1
                break;
38
            default:
39 3
                return $this->executeStalledOther($input, $output);
40
        }
41
42 2
        return 0;
43
    }
44
45 3 View Code Duplication
    public function executeStalledOther(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47 3
        $container = $this->getContainer();
48 3
        $jobManager = $container->get('dtc_queue.job_manager');
49 3
        $type = $input->getArgument('type');
50
        switch ($type) {
51 3
            case 'stalled':
52 1
                $count = $jobManager->pruneStalledJobs();
53 1
                $output->writeln("$count Stalled Job(s) pruned");
54 1
                break;
55 2
            case 'stalled_runs':
56 1
                $count = $container->get('dtc_queue.run_manager')->pruneStalledRuns();
57 1
                $output->writeln("$count Stalled Job(s) pruned");
58 1
                break;
59
            default:
60 1
                return $this->executeOlder($input, $output);
61
        }
62
63 2
        return 0;
64
    }
65
66 1
    public function executeOlder(InputInterface $input, OutputInterface $output)
67
    {
68 1
        $older = $input->getOption('older');
69 1
        $type = $input->getArgument('type');
70 1
        if (!$older) {
71 1
            $output->writeln('<error>--older must be specified</error>');
72
73 1
            return 1;
74
        }
75 1
        if (!preg_match("/(\d+)([d|m|y|h|i|s]){0,1}$/", $older, $matches)) {
76 1
            $output->writeln('<error>Wrong format for --older</error>');
77
78 1
            return 1;
79
        }
80
81 1
        return $this->pruneOldJobs($matches, $type, $output);
82
    }
83
84
    /**
85
     * @param string[]        $matches
86
     * @param string          $type
87
     * @param OutputInterface $output
88
     *
89
     * @return int
90
     *
91
     * @throws \Exception
92
     */
93 1
    protected function pruneOldJobs(array $matches, $type, OutputInterface $output)
94
    {
95 1
        $durationOrTimestamp = intval($matches[1]);
96 1
        $modifier = isset($matches[2]) ? $matches[2] : null;
97
98 1
        if (!$durationOrTimestamp) {
99
            $output->writeln('<error>No duration or timestamp passed in.</error>');
100
101
            return 1;
102
        }
103 1
        $olderThan = new \DateTime();
104 1
        if (null === $modifier) {
105 1
            $olderThan->setTimestamp($durationOrTimestamp);
106
        } else {
107 1
            $interval = $this->getInterval($modifier, $durationOrTimestamp);
108 1
            $olderThan->sub($interval);
109
        }
110
111 1
        return $this->pruneOlderThan($type, $olderThan, $output);
112
    }
113
114
    /**
115
     * @param string          $type
116
     * @param \DateTime       $olderThan
117
     * @param OutputInterface $output
118
     *
119
     * @return int
120
     *
121
     * @throws \Exception
122
     */
123 1
    protected function pruneOlderThan($type, \DateTime $olderThan, OutputInterface $output)
124
    {
125 1
        $container = $this->getContainer();
126 1
        $typeName = null;
0 ignored issues
show
Unused Code introduced by
$typeName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
        switch ($type) {
128 1
            case 'old':
129 1
                $count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan);
130 1
                $typeName = 'Job';
131 1
                break;
132 1
            case 'old_runs':
133 1
                $count = $container->get('dtc_queue.run_manager')->pruneArchivedRuns($olderThan);
134 1
                $typeName = 'Run';
135 1
                break;
136 1
            case 'old_job_timings':
137 1
                $count = $container->get('dtc_queue.run_manager')->pruneJobTimings($olderThan);
138 1
                $typeName = 'Job Timing';
139 1
                break;
140
            default:
141
                throw new \Exception("Unknown type $type");
142
        }
143 1
        $output->writeln("$count Archived $typeName(s) pruned");
144
145 1
        return 0;
146
    }
147
148
    /**
149
     * Returns the date interval based on the modifier and the duration.
150
     *
151
     * @param string $modifier
152
     * @param int    $duration
153
     *
154
     * @return \DateInterval
155
     *
156
     * @throws \Exception
157
     */
158 1
    protected function getInterval($modifier, $duration)
159
    {
160
        switch ($modifier) {
161 1
            case 'd':
162 1
                $interval = new \DateInterval("P${duration}D");
163 1
                break;
164 1
            case 'm':
165 1
                $interval = new \DateInterval("P${duration}M");
166
                break;
167
            case 'y':
168
                $interval = new \DateInterval("P${duration}Y");
169 1
                break;
170
            default:
171 1
                $interval = $this->getIntervalTime($modifier, $duration);
172
        }
173
174 1
        return $interval;
175
    }
176
177
    /**
178
     * @param string $modifier
179
     * @param int    $duration
180
     *
181
     * @return \DateInterval
182
     *
183
     * @throws \Exception
184
     */
185
    protected function getIntervalTime($modifier, $duration)
186
    {
187
        switch ($modifier) {
188 1
            case 'h':
189 1
                $interval = new \DateInterval("PT${duration}H");
190 1
                break;
191 1
            case 'i':
192 1
                $seconds = $duration * 60;
193 1
                $interval = new \DateInterval("PT${seconds}S");
194 1
                break;
195 1
            case 's':
196 1
                $interval = new \DateInterval("PT${duration}S");
197 1
                break;
198
            default:
199
                throw new \Exception("Unknown duration modifier: $modifier");
200
        }
201
202 1
        return $interval;
203
    }
204
}
205