Completed
Push — master ( c70864...b6d4e5 )
by Dmitry
03:30 queued 27s
created

Application::dispatch()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.9137
c 0
b 0
f 0
cc 6
nc 3
nop 3
crap 6.288
1
<?php
2
3
namespace Basis;
4
5
use League\Container\Container;
6
use League\Container\ReflectionContainer;
7
use Tarantool\Mapper\Mapper;
8
use Tarantool\Mapper\Plugin\Annotation;
9
use Tarantool\Mapper\Plugin\Procedure as ProcedurePlugin;
10
use Tarantool\Mapper\Procedure;
11
use Tarantool\Mapper\Repository;
12
13
class Application extends Container
14
{
15
    public function __construct(string $root)
16
    {
17
        parent::__construct();
18
19
        $fs = new Filesystem($this, $root);
20
21
        $this->share(Application::class, $this);
22
        $this->share(Container::class, $this);
23
        $this->share(Filesystem::class, $fs);
24
25
        $this->addServiceProvider(Provider\ClickhouseProvider::class);
26
        $this->addServiceProvider(Provider\CoreProvider::class);
27
        $this->addServiceProvider(Provider\GuzzleProvider::class);
28
        $this->addServiceProvider(Provider\PoolProvider::class);
29
        $this->addServiceProvider(Provider\PredisProvider::class);
30
        $this->addServiceProvider(Provider\ServiceProvider::class);
31
        $this->addServiceProvider(Provider\TarantoolProvider::class);
32
33
        foreach ($fs->listClasses('Provider') as $provider) {
34
            $this->addServiceProvider($provider);
35
        }
36
37
        $this->delegate($this->reflection = new ReflectionContainer());
0 ignored issues
show
Bug introduced by
The property reflection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
    }
39
40 45
    public function dispatch(string $job, array $params = [], string $service = null)
41
    {
42 45
        if ($service !== null) {
43
            if ($this->get(Service::class)->getName() == $service) {
44
                $service = null;
45
            }
46
        }
47
48 45
        return $this->get(Cache::class)
49 45
            ->wrap([$job, $params, $service], function() use ($job, $params, $service) {
50 45
                $runner = $this->get(Runner::class);
51 45
                if ($service === null) {
52 45
                    if ($runner->hasJob($job)) {
53 45
                        return $runner->dispatch($job, $params);
54
                    }
55 1
                    if (explode('.', $job)[0] == $this->get(Service::class)->getName()) {
56
                        return $runner->dispatch($job, $params);
57
                    }
58
                }
59
60 1
                $dispatcher = $this->get(Dispatcher::class);
61 1
                return $dispatcher->dispatch($job, $params, $service);
62 45
            });
63
    }
64
65 45
    public function get($alias, bool $new = false) : object
66
    {
67 45
        if (!$this->has($alias)) {
68 45
            $instance = null;
69 45
            if (is_subclass_of($alias, Procedure::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Procedure::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
70 4
                $instance = $this->get(Mapper::class)
71 4
                    ->getPlugin(ProcedurePlugin::class)
72 4
                    ->get($alias);
73
            }
74 45
            if (is_subclass_of($alias, Repository::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Repository::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
75 1
                $spaceName = $this->get(Mapper::class)
76 1
                    ->getPlugin(Annotation::class)
77 1
                    ->getRepositorySpaceName($alias);
78
79 1
                if ($spaceName) {
80 1
                    $instance = $this->get(Mapper::class)->getRepository($spaceName);
81
                }
82
            }
83 45
            if ($instance) {
84 5
                $this->share($alias, function () use ($instance) {
85 5
                    return $instance;
86 5
                });
87
            }
88
        }
89 45
        return parent::get($alias, $new);
90
    }
91
92 45
    public function has($id) : bool
93
    {
94 45
        if ($this->definitions->has($id)) {
95 45
            return true;
96
        }
97 45
        if ($this->definitions->hasTag($id)) {
98
            return true;
99
        }
100 45
        if ($this->providers->provides($id)) {
101 45
            return true;
102
        }
103 45
        return false;
104
    }
105
106 45
    public function call($callback)
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108 45
        return $this->reflection->call(...func_get_args());
109
    }
110
}
111