Completed
Push — master ( e32f5d...09c5b8 )
by Dmitry
02:45
created

Application::dispatch()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

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