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   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 87
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B map() 0 35 6
A getJobsFromCommand() 0 10 2
A hasCommand() 0 4 1
A getCommand() 0 8 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