GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 75bf3e...f8f0a7 )
by Cees-Jan
01:45
created

Mapper::getJobsFromCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\Tactician\JobCommand;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\Reader;
7
use Exception;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use ReflectionClass;
11
use WyriHaximus\Tactician\JobCommand\Annotations\Job;
12
13
final class Mapper
14
{
15
    /**
16
     * @var array
17
     */
18
    private $map = [];
19
20
    /**
21
     * @param string $path
22
     * @param string $namespace
23
     * @return Mapper
24
     */
25 1
    public function map(string $path, string $namespace): Mapper
26
    {
27 1
        $reader = new AnnotationReader();
28
29 1
        $directory = new RecursiveDirectoryIterator($path);
30 1
        $directory = new RecursiveIteratorIterator($directory);
31
32 1
        foreach ($directory as $node) {
33 1
            if (!is_file($node->getPathname())) {
34 1
                continue;
35
            }
36
37 1
            $file = substr($node->getPathname(), strlen($path));
38 1
            $file = ltrim($file, DIRECTORY_SEPARATOR);
39 1
            $file = rtrim($file, '.php');
40
41 1
            $class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file);
42
43 1
            if (!class_exists($class)) {
44
                continue;
45
            }
46
47 1
            $jobs = self::getJobsFromCommand($class, $reader);
48
49 1
            if (count($jobs) === 0) {
50
                continue;
51
            }
52
53 1
            foreach ($jobs as $job) {
54 1
                $this->map[$job] = $class;
55
            }
56
        }
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @param string $command
63
     * @param Reader $reader
64
     * @return array
65
     */
66 4
    public function getJobsFromCommand(string $command, Reader $reader): array
67
    {
68 4
        $annotation = $reader->getClassAnnotation(new ReflectionClass($command), Job::class);
69
70 4
        if (!($annotation instanceof Job)) {
71 1
            return [];
72
        }
73
74 3
        return $annotation->getJobs();
75
    }
76
77
    /**
78
     * @param string $job
79
     * @return bool
80
     */
81 1
    public function hasCommand(string $job): bool
82
    {
83 1
        return isset($this->map[$job]);
84
    }
85
86
    /**
87
     * @param string $job
88
     * @return string
89
     * @throws Exception
90
     */
91 2
    public function getCommand(string $job): string
92
    {
93 2
        if (isset($this->map[$job])) {
94 1
            return $this->map[$job];
95
        }
96
97 1
        throw new Exception('No command known for job: ' . $job);
98
    }
99
}
100