Completed
Push — master ( 8260b5...6e390f )
by Dmitry
08:05
created

Runner   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 11.36%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 86
ccs 5
cts 44
cp 0.1136
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getMapping() 0 25 5
A hasJob() 0 4 1
A getJobClass() 0 14 3
A dispatch() 0 15 3
A castArguments() 0 13 3
1
<?php
2
3
namespace Basis;
4
5
use LogicException;
6
use League\Container\Container;
7
use ReflectionClass;
8
use ReflectionProperty;
9
10
class Runner
11
{
12
    private $app;
13
    private $mapping;
14
15 2
    public function __construct(Application $app)
16
    {
17 2
        $this->app = $app;
18 2
    }
19
20
    public function getMapping()
21
    {
22
        if (!$this->mapping) {
23
            $jobs = [];
24
            foreach ($this->app->get(Framework::class)->listClasses('Job') as $class) {
25
                $jobs[substr($class, strlen('Basis\\Job\\'))] = $class;
26
            }
27
            $serviceName = ucfirst($this->app->get(Config::class)['service']);
28
            foreach ($this->app->get(Filesystem::class)->listClasses('Job') as $class) {
29
                $jobs[$serviceName.'\\'.substr($class, strlen('Job\\'))] = $class;
30
                $jobs[substr($class, strlen('Job\\'))] = $class;
31
            }
32
33
34
            foreach ($jobs as $part => $class) {
35
                $nick = str_replace('\\', '.', $part);
36
                $jobs[$nick] = $class;
37
                $jobs[strtolower($nick)] = $class;
38
            }
39
40
            $this->mapping = $jobs;
41
        }
42
43
        return $this->mapping;
44
    }
45
46 2
    public function hasJob($nick)
47
    {
48 2
        return array_key_exists($nick, $mapping);
0 ignored issues
show
Bug introduced by
The variable $mapping does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
    }
50
51
    public function getJobClass($nick)
52
    {
53
        $mapping = $this->getMapping();
54
        if (!array_key_exists($nick, $mapping)) {
55
            throw new LogicException("No job $nick");
56
        }
57
58
        $class = $mapping[$nick];
59
60
        if (!class_exists($class)) {
61
            throw new LogicException("No class for job $nick");
62
        }
63
        return $class;
64
    }
65
66
    public function dispatch($nick, $arguments = [])
67
    {
68
        $class = $this->getJobClass($nick);
69
70
        $instance = $this->app->get($class);
71
        if (array_key_exists(0, $arguments)) {
72
            $arguments = $this->castArguments($class, $arguments);
73
        }
74
75
        foreach ($arguments as $k => $v) {
76
            $instance->$k = $v;
77
        }
78
79
        return $this->app->call([$instance, 'run']);
80
    }
81
82
    private function castArguments($class, $arguments)
83
    {
84
        $reflection = new ReflectionClass($class);
85
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
86
        if (count($properties) == 1) {
87
            return [
88
                $properties[0]->getName() => count($arguments) == 1
89
                    ? $arguments[0]
90
                    : implode(' ', $arguments)
91
            ];
92
        }
93
        return $arguments;
94
    }
95
}
96