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

CronTasksFinder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 44
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getCronTasks() 0 34 5
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
  }