Completed
Push — master ( c85e73...41284f )
by Dmitry
02:55
created

Runner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 26
    public function __construct(Application $app)
16
    {
17 26
        $this->app = $app;
18 26
    }
19
20 26
    public function getMapping() : array
21
    {
22 26
        if (!$this->mapping) {
23 26
            $jobs = [];
24 26
            foreach ($this->app->get(Framework::class)->listClasses('Job') as $class) {
25 26
                $jobs[substr($class, strlen('Basis\\Job\\'))] = $class;
26
            }
27 26
            $serviceName = ucfirst($this->app->get(Config::class)['service']);
28 26
            foreach ($this->app->get(Filesystem::class)->listClasses('Job') as $class) {
29 26
                $jobs[$serviceName.'\\'.substr($class, strlen('Job\\'))] = $class;
30 26
                $jobs[$serviceName.'\\'.lcfirst(substr($class, strlen('Job\\')))] = $class;
31 26
                $jobs[substr($class, strlen('Job\\'))] = $class;
32 26
                $jobs[lcfirst(substr($class, strlen('Job\\')))] = $class;
33
            }
34
35
36 26
            foreach ($jobs as $part => $class) {
37 26
                $nick = str_replace('\\', '.', $part);
38 26
                $jobs[$nick] = $class;
39 26
                $jobs[lcfirst($nick)] = $class;
40 26
                $jobs[strtolower($nick)] = $class;
41
            }
42
43 26
            $this->mapping = $jobs;
44
        }
45
46 26
        return $this->mapping;
47
    }
48
49 26
    public function hasJob(string $nick) : bool
50
    {
51 26
        return array_key_exists($nick, $this->getMapping());
52
    }
53
54 26
    public function getJobClass(string $nick) : string
55
    {
56 26
        $mapping = $this->getMapping();
57 26
        if (!array_key_exists($nick, $mapping)) {
58
            throw new LogicException("No job $nick");
59
        }
60
61 26
        $class = $mapping[$nick];
62
63 26
        if (!class_exists($class)) {
64
            throw new LogicException("No class for job $nick");
65
        }
66 26
        return $class;
67
    }
68
69 26
    public function dispatch(string $nick, array $arguments = [])
70
    {
71 26
        if (count($arguments)) {
72 5
            $converter = $this->app->get(Converter::class);
73 5
            if ($converter->isTuple($arguments) && is_array($arguments[0])) {
74 1
                $result = [];
75 1
                foreach ($arguments as $params) {
76 1
                    $result[] = $this->dispatch($nick, $params);
77
                }
78 1
                return $result;
79
            }
80
        }
81
82 26
        $class = $this->getJobClass($nick);
83
84 26
        $instance = $this->app->get($class);
85 26
        if (array_key_exists(0, $arguments)) {
86 1
            $arguments = $this->castArguments($class, $arguments);
87
        }
88
89 26
        foreach ($arguments as $k => $v) {
90 5
            $instance->$k = $v;
91
        }
92
93 26
        return $this->app->get(Converter::class)->toObject($this->app->call([$instance, 'run']));
94
    }
95
96 1
    private function castArguments(string $class, array $arguments) : array
97
    {
98 1
        $reflection = new ReflectionClass($class);
99 1
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
100 1
        if (count($properties) == 1) {
101
            return [
102 1
                $properties[0]->getName() => count($arguments) == 1
103 1
                    ? $arguments[0]
104 1
                    : implode(' ', $arguments)
105
            ];
106
        }
107
        return $arguments;
108
    }
109
}
110