Completed
Push — master ( 382c80...0287cb )
by Dmitry
04:23
created

Runner   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 86
ccs 40
cts 44
cp 0.9091
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 16 3
A castArguments() 0 13 3
A __construct() 0 4 1
B getMapping() 0 25 4
A getJobClass() 0 18 4
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 3
    public function __construct(Application $app)
16
    {
17 3
        $this->app = $app;
18 3
    }
19
20 3
    public function getMapping()
21
    {
22 3
        if(!$this->mapping) {
23
24 3
            $classes = array_merge(
25 3
                $this->app->get(Filesystem::class)->listClasses('Jobs'),
26 3
                $this->app->get(Framework::class)->listClasses('Jobs')
27
            );
28
29 3
            $jobs = [];
30 3
            foreach($classes as $class) {
31 3
                list($name, $group) = array_map('lcfirst', array_reverse(explode("\\", $class)));
32 3
                $nick = "$group.$name";
33
34 3
                if(!array_key_exists($nick, $jobs)) {
35 3
                    $jobs[$nick] = $class;
36 3
                    $jobs[strtolower($nick)] = $class;
37
                }
38
            }
39
40 3
            $this->mapping = $jobs;
41
        }
42
43 3
        return $this->mapping;
44
    }
45
46 3
    public function getJobClass($nick)
47
    {
48 3
        if(!strstr($nick, '.')) {
49
            throw new LogicException("Incorrect nick - $nick");
50
        }
51
52 3
        $mapping = $this->getMapping();
53 3
        if(!array_key_exists($nick, $mapping)) {
54
            throw new LogicException("No job $nick");
55
        }
56
57 3
        $class = $mapping[$nick];
58
59 3
        if(!class_exists($class)) {
60
            throw new LogicException("No class for job $nick");
61
        }
62 3
        return $class;
63
    }
64
65 3
    public function dispatch($nick, $arguments = [])
66
    {
67 3
        $class = $this->getJobClass($nick);
68
69 3
        $instance = $this->app->get($class);
70 3
        if(array_key_exists(0, $arguments)) {
71 1
            $arguments = $this->castArguments($class, $arguments);
72
        }
73
74 3
        foreach($arguments as $k => $v) {
75 2
            $instance->$k = $v;
76
        }
77
78 3
        $container = $this->app->get(Container::class);
79 3
        return $container->call([$instance, 'run']);
80
    }
81
82 1
    private function castArguments($class, $arguments)
83
    {
84 1
        $reflection = new ReflectionClass($class);
85 1
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
86 1
        if(count($properties) == 1) {
87
            return [
88 1
                $properties[0]->getName() => count($arguments) == 1
89 1
                    ? $arguments[0]
90 1
                    : implode(' ', $arguments)
91
            ];
92
        }
93
        return $arguments;
94
    }
95
}
96