Completed
Push — master ( 073c6d...49d4ca )
by Dmitry
06:07
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 59.57%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 74
ccs 28
cts 47
cp 0.5957
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 24 2
A dispatch() 0 19 5
B get() 0 26 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
    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 && $runner->hasJob($job)) {
52 45
                    return $runner->dispatch($job, $params);
53
                }
54
55 1
                $dispatcher = $this->get(Dispatcher::class);
56 1
                return $dispatcher->dispatch($job, $params, $service);
57 45
            });
58
    }
59
60 45
    public function get($alias, array $args = [])
61
    {
62 45
        if (!$this->hasShared($alias, true)) {
63 45
            $instance = null;
64 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...
65 4
                $instance = $this->get(Mapper::class)
66 4
                    ->getPlugin(ProcedurePlugin::class)
67 4
                    ->get($alias);
68
            }
69 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...
70 1
                $spaceName = $this->get(Mapper::class)
71 1
                    ->getPlugin(Annotation::class)
72 1
                    ->getRepositorySpaceName($alias);
73
74 1
                if ($spaceName) {
75 1
                    $instance = $this->get(Mapper::class)->getRepository($spaceName);
76
                }
77
            }
78 45
            if ($instance) {
79 5
                $this->share($alias, function () use ($instance) {
80 5
                    return $instance;
81 5
                });
82
            }
83
        }
84 45
        return parent::get($alias, $args);
85
    }
86
}
87