Completed
Push — master ( 42afef...5f18df )
by Dmitry
06:49 queued 03:50
created

Runner::dispatch()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0322

Importance

Changes 6
Bugs 0 Features 5
Metric Value
c 6
b 0
f 5
dl 0
loc 35
ccs 21
cts 23
cp 0.913
rs 6.7272
cc 7
eloc 19
nc 16
nop 2
crap 7.0322
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
14 4
    function __construct(Application $app)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16 4
        $this->app = $app;
17 4
    }
18
19 1
    function listJobs()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21 1
        $classes = array_merge(
22 1
            $this->app->get(Filesystem::class)->listClasses('Jobs'),
23 1
            $this->app->get(Framework::class)->listClasses('Jobs')
24 1
        );
25
26 1
        $jobs = [];
27 1
        foreach($classes as $class) {
28 1
            list($name, $group) = array_map('strtolower', array_reverse(explode("\\", $class)));
29 1
            $nick = "$group.$name";
30
31 1
            if(!array_key_exists($nick, $jobs)) {
32 1
                $jobs[$nick] = $class;
33 1
            }
34 1
        }
35
36 1
        return $jobs;
37
    }
38
39 4
    function dispatch($nick, $arguments = [])
40
    {
41 4
        if(!strstr($nick, '.')) {
42
            throw new LogicException("Incorrect nick - $nick");
43
        }
44
45 4
        list($group, $name) = array_map('ucfirst', explode('.', $nick));
46
47 4
        $className = "Jobs\\$group\\$name";
48
49 4
        $class = $this->app->get(Filesystem::class)->completeClassName($className);
50
51 4
        if(!class_exists($class)) {
52 3
            $frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
53 3
            if(class_exists($frameworkClass)) {
54 3
                $class = $frameworkClass;
55 3
            }
56 3
        }
57
58 4
        if(!class_exists($class)) {
59
            throw new LogicException("No job $nick");
60
        }
61
62 4
        $instance = $this->app->get($class);
63 4
        if(array_key_exists(0, $arguments)) {
64 2
            $arguments = $this->castArguments($class, $arguments);
65 2
        }
66
67 4
        foreach($arguments as $k => $v) {
68 3
            $instance->$k = $v;
69 4
        }
70
71 4
        $container = $this->app->get(Container::class);
72 4
        return $container->call([$instance, 'run']);
73
    }
74
75 2
    private function castArguments($class, $arguments)
76
    {
77 2
        $reflection = new ReflectionClass($class);
78 2
        $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
79 2
        if(count($properties) == 1) {
80
            return [
81 2
                $properties[0]->getName() => count($arguments) == 1
82 2
                    ? $arguments[0]
83 2
                    : implode(' ', $arguments)
84 2
            ];
85
        }
86
        return $arguments;
87
    }
88
}
89