Completed
Push — master ( e13227...d1ea0c )
by Dmitry
03:01
created

Application::get()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 17
nc 13
nop 2
crap 6
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 16
    public function __construct(string $root)
16
    {
17 16
        parent::__construct();
18
19 16
        $fs = new Filesystem($this, $root);
20
21 16
        $this->share(Application::class, $this);
22 16
        $this->share(Container::class, $this);
23 16
        $this->share(Filesystem::class, $fs);
24
25 16
        $this->addServiceProvider(Provider\CoreProvider::class);
26 16
        $this->addServiceProvider(Provider\ServiceProvider::class);
27 16
        $this->addServiceProvider(Provider\TarantoolProvider::class);
28
29 16
        foreach ($fs->listClasses('Provider') as $provider) {
30 16
            $this->addServiceProvider($provider);
31
        }
32
33 16
        $this->delegate(new ReflectionContainer());
34 16
    }
35
36 10
    public function dispatch(string $job, array $params = [], string $service = null)
37
    {
38 10
        if ($service !== null) {
39
            if ($this->get(Service::class)->getName() == $service) {
40
                $service = null;
41
            }
42
        }
43
44 10
        $runner = $this->get(Runner::class);
45 10
        if ($service === null && $runner->hasJob($job)) {
46 10
            return $runner->dispatch($job, $params);
47
        }
48
49
        $dispatcher = $this->get(Dispatcher::class);
50
        return $dispatcher->dispatch($job, $params, $service);
51
    }
52
53 16
    public function get($alias, array $args = [])
54
    {
55 16
        if (!$this->hasShared($alias, true)) {
56 14
            $instance = null;
57 14
            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...
58 1
                $instance = $this->get(Mapper::class)
59 1
                    ->getPlugin(ProcedurePlugin::class)
60 1
                    ->get($alias);
61
            }
62 14
            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...
63 1
                $spaceName = $this->get(Mapper::class)
64 1
                    ->getPlugin(Annotation::class)
65 1
                    ->getRepositorySpaceName($alias);
66
67 1
                if ($spaceName) {
68 1
                    $instance = $this->get(Mapper::class)->getRepository($spaceName);
69
                }
70
            }
71 14
            if ($instance) {
72 2
                $this->share($alias, function () use ($instance) {
73 2
                    return $instance;
74 2
                });
75
            }
76
        }
77
        return parent::get($alias, $args);
78
    }
79
}
80