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

Runner   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.88%

Importance

Changes 6
Bugs 0 Features 5
Metric Value
wmc 14
c 6
b 0
f 5
lcom 1
cbo 1
dl 0
loc 79
ccs 46
cts 49
cp 0.9388
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A listJobs() 0 19 3
C dispatch() 0 35 7
A castArguments() 0 13 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
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