Completed
Push — master ( 601de6...446929 )
by Dmitry
04:40
created

Runner::getJobClass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

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