Discovery::discoverTaskTypes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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