Completed
Push — master ( e20be0...8ab58f )
by Dmitry
02:48
created

Application::dispatch()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.9137
c 0
b 0
f 0
cc 6
nc 3
nop 3
crap 6.288
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(new ReflectionContainer());
38
    }
39
40 45
    public function dispatch(string $job, array $params = [], string $service = null)
41
    {
42 45
        if ($service !== null) {
43
            if ($this->get(Service::class)->getName() == $service) {
44
                $service = null;
45
            }
46
        }
47
48 45
        return $this->get(Cache::class)
49 45
            ->wrap([$job, $params, $service], function() use ($job, $params, $service) {
50 45
                $runner = $this->get(Runner::class);
51 45
                if ($service === null) {
52 45
                    if ($runner->hasJob($job)) {
53 45
                        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 45
            });
63
    }
64
65 45
    public function get($alias, array $args = [])
66
    {
67 45
        if (!$this->hasShared($alias, true)) {
68 45
            $instance = null;
69 45
            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 4
                $instance = $this->get(Mapper::class)
71 4
                    ->getPlugin(ProcedurePlugin::class)
72 4
                    ->get($alias);
73
            }
74 45
            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 45
            if ($instance) {
84 5
                $this->share($alias, function () use ($instance) {
85 5
                    return $instance;
86 5
                });
87
            }
88
        }
89 45
        return parent::get($alias, $args);
90
    }
91
}
92