1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Plugins; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Tarantool\Mapper\Entity; |
7
|
|
|
use Tarantool\Mapper\Plugin; |
8
|
|
|
use Tarantool\Mapper\Repository; |
9
|
|
|
use Tarantool\Mapper\Space; |
10
|
|
|
|
11
|
|
|
class UserClasses extends Plugin |
12
|
|
|
{ |
13
|
|
|
private $repositories = []; |
14
|
|
|
private $entities = []; |
15
|
|
|
|
16
|
|
|
public function getRepositoryClass(Space $space) |
17
|
|
|
{ |
18
|
|
|
if(array_key_exists($space->getName(), $this->repositories)) { |
19
|
|
|
return $this->repositories[$space->getName()]; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getEntityClass(Space $space) |
24
|
|
|
{ |
25
|
|
|
if(array_key_exists($space->getName(), $this->entities)) { |
26
|
|
|
return $this->entities[$space->getName()]; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function mapEntity($space, $class) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->validateSpace($space); |
33
|
|
|
|
34
|
|
|
if(!class_exists($class)) { |
35
|
|
|
throw new Exception("No class $class"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if(!is_subclass_of($class, Entity::class)) { |
|
|
|
|
39
|
|
|
throw new Exception("Entity should extend " . Entity::class . " class"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->entities[$space] = $class; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function mapRepository($space, $class) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->validateSpace($space); |
48
|
|
|
|
49
|
|
|
if(!class_exists($class)) { |
50
|
|
|
throw new Exception("No class $class"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if(!is_subclass_of($class, Repository::class)) { |
|
|
|
|
54
|
|
|
throw new Exception("Repository should extend " . Repository::class . " class"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->repositories[$space] = $class; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function validateSpace($space) |
61
|
|
|
{ |
62
|
|
|
if(!$this->mapper->getSchema()->hasSpace($space)) { |
63
|
|
|
throw new Exception("No space $space"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.