Completed
Push — master ( 8fe865 )
by Dmitry
04:10
created

Mapper::addPlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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
14
    public function __construct(Client $client)
15
    {
16
        $this->client = $client;
17
    }
18
19
    public function addPlugin($class)
20
    {
21
        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...
22
            throw new Exception("Plugin should extend " . Plugin::class . " class");
23
        }
24
25
        $plugin = new $class($this);
26
        $this->plugins[] = $plugin;
27
28
        return $plugin;
29
    }
30
31
    public function create($space, $data)
32
    {
33
        $instance = $this->getRepository($space)->create($data);
34
        $this->getRepository($space)->save($instance);
35
        return $instance;
36
    }
37
38
    public function findOne($space, $params = [])
39
    {
40
        return $this->getRepository($space)->findOne($params);
41
    }
42
43
    public function find($space, $params = [])
44
    {
45
        return $this->getRepository($space)->find($params);
46
    }
47
48
    public function findRepository(Entity $instance)
49
    {
50
        foreach($this->getSchema()->getSpaces() as $space) {
51
            if($space->getRepository()->knows($instance)) {
52
                return $space->getRepository();
53
            }
54
        }
55
56
        throw new Exception("No Repository for given Entity");
57
    }
58
59
    public function getClient()
60
    {
61
        return $this->client;
62
    }
63
64
    public function getPlugins()
65
    {
66
        return $this->plugins;
67
    }
68
69
    public function getRepository($space)
70
    {
71
        return $this->getSchema()->getSpace($space)->getRepository();
72
    }
73
74
    public function getSchema()
75
    {
76
        return $this->schema ?: $this->schema = new Schema($this);
77
    }
78
79
    public function remove(Entity $instance)
80
    {
81
        $this->findRepository($instance)->remove($instance);
82
    }
83
84
    public function save(Entity $instance)
85
    {
86
        $this->findRepository($instance)->save($instance);
87
    }
88
}