Completed
Push — master ( 15fe5a...ea8146 )
by Dmitry
10:23
created

Application::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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