Completed
Push — master ( 13738a...da5e92 )
by Dmitry
07:09 queued 05:43
created

Application::get()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 18
cp 0.8333
rs 8.8817
c 0
b 0
f 0
cc 6
nc 13
nop 2
crap 6.1666
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 38
    public function dispatch(string $job, array $params = [], string $service = null)
41
    {
42 38
        if ($service !== null) {
43
            if ($this->get(Service::class)->getName() == $service) {
44
                $service = null;
45
            }
46
        }
47
48 38
        return $this->get(Cache::class)
49 38
            ->wrap([$job, $params, $service], function() use ($job, $params, $service) {
50 38
                $runner = $this->get(Runner::class);
51 38
                if ($service === null) {
52 38
                    if ($runner->hasJob($job)) {
53 38
                        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 38
            });
63
    }
64
65 38
    public function get($alias, bool $new = false) : object
66
    {
67 38
        if (!$this->has($alias)) {
68 38
            $instance = null;
69 38
            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
                $instance = $this->get(Mapper::class)
71
                    ->getPlugin(ProcedurePlugin::class)
72
                    ->get($alias);
73
            }
74 38
            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 38
            if ($instance) {
84 1
                $this->share($alias, function () use ($instance) {
85 1
                    return $instance;
86 1
                });
87
            }
88
        }
89 38
        return parent::get($alias, $new);
90
    }
91
92 38
    public function has($id) : bool
93
    {
94 38
        if ($this->definitions->has($id)) {
95 38
            return true;
96
        }
97 38
        if ($this->definitions->hasTag($id)) {
98
            return true;
99
        }
100 38
        if ($this->providers->provides($id)) {
101 38
            return true;
102
        }
103 38
        return false;
104
    }
105
106 38
    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 38
        return $this->reflection->call(...func_get_args());
109
    }
110
}
111