Completed
Push — master ( 8e2e97...8fb2e6 )
by Shcherbak
01:59
created

CronTasksFinder::getCronTasks()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 6
nop 2
crap 30
1
<?php
2
3
  namespace Funivan\Console\CommandCron;
4
5
  use DocBlockReader\Reader;
6
  use Symfony\Component\Console\Application;
7
8
9
  /**
10
   * @author Ivan Shcherbak <[email protected]>
11
   */
12
  class CronTasksFinder {
13
14
15
    /**
16
     * @param Application $application
17
     * @param callable|null $annotationFinder
18
     * @return CronTaskInfo[]
19
     */
20
    public function getCronTasks(Application $application, callable $annotationFinder = null) {
21
22
      if ($annotationFinder === null) {
23
        $annotationFinder = function (Reader $reader) {
24
          return $reader->getParameter('crontab');
25
        };
26
      }
27
28
      $tasks = [];
29
30
      foreach ($application->all() as $command) {
31
        $reader = new \DocBlockReader\Reader(get_class($command));
32
33
        $crontabAnnotations = $annotationFinder($reader);
34
35
        if (empty($crontabAnnotations)) {
36
          continue;
37
        }
38
39
        $crontabAnnotations = (array) $crontabAnnotations;
40
41
        foreach ($crontabAnnotations as $crontabExpression) {
42
          $commandArguments = preg_split('!\s+!', $crontabExpression, -1, PREG_SPLIT_NO_EMPTY);
43
          array_splice($commandArguments, 0, 5);
44
45
          $commandArguments = implode(' ', $commandArguments);
46
          $preparedExpression = trim(str_replace([" /", $commandArguments], [' */', ''], " " . $crontabExpression));
47
          $tasks[] = new CronTaskInfo($preparedExpression, $command, $commandArguments);
48
49
        }
50
      }
51
52
      return $tasks;
53
    }
54
55
  }