Completed
Push — master ( d3e72f...c0203d )
by Dmitry
01:39
created

Mapper::getMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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[get_class($plugin)] = $plugin;
28
29
        return $plugin;
30
    }
31
32
    public function create($space, $data)
33
    {
34
        return $this->getRepository($space)->create($data)->save();
35
    }
36
37
    public function findOne($space, $params = [])
38
    {
39
        return $this->getRepository($space)->findOne($params);
40
    }
41
42
    public function findOrCreate($space, $params = [])
43
    {
44
        return $this->getRepository($space)->findOrCreate($params)->save();
45
    }
46
47
    public function findOrFail($space, $params = [])
48
    {
49
        return $this->getRepository($space)->findOrFail($params)->save();
50
    }
51
52
    public function find($space, $params = [])
53
    {
54
        return $this->getRepository($space)->find($params);
55
    }
56
57
    public function findRepository(Entity $instance)
58
    {
59
        foreach ($this->getSchema()->getSpaces() as $space) {
60
            if ($space->getRepository()->knows($instance)) {
61
                return $space->getRepository();
62
            }
63
        }
64
65
        throw new Exception("No Repository for given Entity");
66
    }
67
68
    public function getBootstrap()
69
    {
70
        return $this->bootstrap ?: $this->bootstrap = new Bootstrap($this);
71
    }
72
73
    public function getClient()
74
    {
75
        return $this->client;
76
    }
77
78
    public function getMeta()
79
    {
80
        return [
81
            'schema' => $this->getSchema()->getMeta(),
82
        ];
83
    }
84
85
    public function getPlugin($class)
86
    {
87
        if (!array_key_exists($class, $this->plugins)) {
88
            throw new Exception("No plugin $class");
89
        }
90
        return $this->plugins[$class];
91
    }
92
93
    public function hasPlugin($class)
94
    {
95
        return array_key_exists($class, $this->plugins);
96
    }
97
98
    public function getPlugins()
99
    {
100
        return array_values($this->plugins);
101
    }
102
103
    public function getRepository($space)
104
    {
105
        return $this->getSchema()->getSpace($space)->getRepository();
106
    }
107
108
    public function getRepositories()
109
    {
110
        $repositories = [];
111
        foreach ($this->getSchema()->getSpaces() as $space) {
112
            if ($space->repositoryExists()) {
113
                $repositories[] = $space->getRepository();
114
            }
115
        }
116
        return $repositories;
117
    }
118
119
    public function getSchema()
120
    {
121
        return $this->schema ?: $this->schema = new Schema($this);
122
    }
123
124
    public function remove($space, $params = [])
125
    {
126
        if ($space instanceof Entity) {
127
            $this->findRepository($space)->removeEntity($space);
128
        } else {
129
            $this->getRepository($space)->remove($params);
130
        }
131
    }
132
133
    public function save(Entity $instance)
134
    {
135
        $this->findRepository($instance)->save($instance);
136
    }
137
138
    public function setMeta($meta)
139
    {
140
        if ($this->schema) {
141
            $this->schema->setMeta($meta['schema']);
0 ignored issues
show
Bug introduced by
The method setMeta() does not seem to exist on object<Tarantool\Mapper\Schema>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
        } else {
143
            $this->schema = new Schema($this, $meta['schema']);
144
        }
145
    }
146
}
147