Completed
Push — master ( 3e8a03...43ff2c )
by Dmitry
02:19
created

Mapper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
lcom 3
cbo 2
dl 0
loc 111
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 6 1
A findOne() 0 4 1
A find() 0 4 1
A findRepository() 0 10 3
A getClient() 0 4 1
A getBootstrap() 0 8 2
A addPlugin() 0 11 3
A getPlugin() 0 8 3
A getPlugins() 0 4 1
A getRepository() 0 4 1
A getRepositories() 0 10 3
A getSchema() 0 4 2
A remove() 0 4 1
A save() 0 4 1
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Tarantool\Client\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tarantool\Mapper\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Exception;
7
8
class Mapper
9
{
10
    private $client;
11
    private $plugins = [];
12
    private $schema;
13
    private $bootstrap;
14
15
    public function __construct(Client $client)
16
    {
17
        $this->client = $client;
18
    }
19
20
    public function addPlugin($class)
21
    {
22
        if(!is_subclass_of($class, Plugin::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\Plugin::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
23
            throw new Exception("Plugin should extend " . Plugin::class . " class");
24
        }
25
26
        $plugin = is_object($class) ? $class : new $class($this);
27
        $this->plugins[] = $plugin;
28
29
        return $plugin;
30
    }
31
32
    public function create($space, $data)
33
    {
34
        $instance = $this->getRepository($space)->create($data);
35
        $this->getRepository($space)->save($instance);
36
        return $instance;
37
    }
38
39
    public function findOne($space, $params = [])
40
    {
41
        return $this->getRepository($space)->findOne($params);
42
    }
43
44
    public function find($space, $params = [])
45
    {
46
        return $this->getRepository($space)->find($params);
47
    }
48
49
    public function findRepository(Entity $instance)
50
    {
51
        foreach($this->getSchema()->getSpaces() as $space) {
52
            if($space->getRepository()->knows($instance)) {
53
                return $space->getRepository();
54
            }
55
        }
56
57
        throw new Exception("No Repository for given Entity");
58
    }
59
60
    public function getBootstrap()
61
    {
62
        if(!$this->bootstrap) {
63
            $this->bootstrap = new Bootstrap($this);
64
        }
65
66
        return $this->bootstrap;
67
    }
68
69
    public function getClient()
70
    {
71
        return $this->client;
72
    }
73
74
    public function getPlugin($class)
75
    {
76
        foreach($this->getPlugins() as $plugin) {
77
            if(is_a($plugin, $class)) {
78
                return $plugin;
79
            }
80
        }
81
    }
82
83
    public function getPlugins()
84
    {
85
        return $this->plugins;
86
    }
87
88
    public function getRepository($space)
89
    {
90
        return $this->getSchema()->getSpace($space)->getRepository();
91
    }
92
93
    public function getRepositories()
94
    {
95
        $repositories = [];
96
        foreach($this->getSchema()->getSpaces() as $space) {
97
            if($space->repositoryExists()) {
98
                $repositories[] = $space->getRepository();
99
            }
100
        }
101
        return $repositories;
102
    }
103
104
    public function getSchema()
105
    {
106
        return $this->schema ?: $this->schema = new Schema($this);
107
    }
108
109
    public function remove(Entity $instance)
110
    {
111
        $this->findRepository($instance)->remove($instance);
112
    }
113
114
    public function save(Entity $instance)
115
    {
116
        $this->findRepository($instance)->save($instance);
117
    }
118
}
119