Test Failed
Pull Request — master (#7)
by Jim
03:47
created

Discovery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getTasks() 0 8 2
A discoverTaskTypes() 0 23 3
1
<?php
2
3
4
namespace Jarobe\TaskRunnerBundle\Hydrator;
5
6
use Doctrine\Common\Annotations\Reader;
7
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface;
8
use Jarobe\TaskRUnner\Annotation\TaskType;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
13
class Discovery implements DiscoveryInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $namespace;
19
20
    /**
21
     * @var string
22
     */
23
    private $directory;
24
25
    /**
26
     * The Kernel root directory
27
     * @var string
28
     */
29
    private $rootDir;
30
31
    /**
32
     * @var array|null
33
     */
34
    private $tasks;
35
36
37
    /**
38
     * WorkerDiscovery constructor.
39
     *
40
     * @param $namespace
41
     *   The namespace of the workers
42
     * @param $directory
43
     *   The directory of the workers
44
     * @param $rootDir
45
     */
46
    public function __construct($namespace, $directory, $rootDir)
47
    {
48
        $this->namespace = $namespace;
49
        $this->directory = $directory;
50
        $this->rootDir = $rootDir;
51
        $this->tasks = null;
52
    }
53
54
    /**
55
     * Returns all the tasks
56
     * @return array
57
     */
58
    public function getTasks()
59
    {
60
        if ($this->tasks === null) {
61
            $this->tasks = $this->discoverTaskTypes();
62
        }
63
64
        return $this->tasks;
65
    }
66
67
    /**
68
     * Discovers tasks
69
     * @return array $tasks
70
     */
71
    private function discoverTaskTypes()
72
    {
73
        $path = $this->rootDir . '/../src/' . $this->directory;
74
        $finder = new Finder();
75
        $finder->files()->in($path);
76
77
        $tasks = [];
78
        /** @var SplFileInfo $file */
79
        foreach ($finder as $file) {
80
            /** @var TaskTypeInterface $class */
81
            $class = $this->namespace . '\\' . $file->getBasename('.php');
82
83
            if (!$class instanceof TaskTypeInterface) {
84
                continue;
85
            }
86
87
            $name = $class::getName();
88
89
            /** @var TaskType $annotation */
90
            $tasks[$name] = $class;
91
        }
92
        return $tasks;
93
    }
94
}