Completed
Push — master ( 717b1b...de6dfe )
by Dmitry
06:37
created

Runner::listJobs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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