Completed
Push — master ( 5e9169...e507aa )
by Dmitry
04:13
created

Application::has()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

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