1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee\AccessRole; |
13
|
|
|
|
14
|
|
|
use Generator; |
15
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
16
|
|
|
use MongoDB\Database; |
17
|
|
|
use Tubee\AccessRole; |
18
|
|
|
use Tubee\Resource\Factory as ResourceFactory; |
19
|
|
|
|
20
|
|
|
class Factory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Collection name. |
24
|
|
|
*/ |
25
|
|
|
public const COLLECTION_NAME = 'access_roles'; |
26
|
|
|
/** |
27
|
|
|
* Database. |
28
|
|
|
* |
29
|
|
|
* @var Database |
30
|
|
|
*/ |
31
|
|
|
protected $db; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Resource factory. |
35
|
|
|
* |
36
|
|
|
* @var ResourceFactory |
37
|
|
|
*/ |
38
|
|
|
protected $resource_factory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initialize. |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Database $db, ResourceFactory $resource_factory) |
44
|
|
|
{ |
45
|
|
|
$this->db = $db; |
46
|
|
|
$this->resource_factory = $resource_factory; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Has resource. |
51
|
|
|
*/ |
52
|
|
|
public function has(string $name): bool |
53
|
|
|
{ |
54
|
|
|
return $this->db->{self::COLLECTION_NAME}->count(['name' => $name]) > 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get resources. |
59
|
|
|
*/ |
60
|
|
|
public function getAll(?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator |
61
|
|
|
{ |
62
|
|
|
$that = $this; |
63
|
|
|
|
64
|
|
|
return $this->resource_factory->getAllFrom($this->db->{self::COLLECTION_NAME}, $query, $offset, $limit, $sort, function ($resource) use ($that) { |
65
|
|
|
return $that->build($resource); |
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get resource. |
71
|
|
|
*/ |
72
|
|
|
public function getOne(string $name): AccessRoleInterface |
73
|
|
|
{ |
74
|
|
|
$result = $this->db->{self::COLLECTION_NAME}->findOne([ |
75
|
|
|
'name' => $name, |
76
|
|
|
], [ |
77
|
|
|
'projection' => ['history' => 0], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
if ($result === null) { |
81
|
|
|
throw new Exception\NotFound('access role '.$name.' is not registered'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->build($result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Delete by name. |
89
|
|
|
*/ |
90
|
|
|
public function deleteOne(string $name): bool |
91
|
|
|
{ |
92
|
|
|
$resource = $this->getOne($name); |
93
|
|
|
$this->resource_factory->deleteFrom($this->db->{self::COLLECTION_NAME}, $resource->getId()); |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add resource. |
100
|
|
|
*/ |
101
|
|
|
public function add(array $resource): ObjectIdInterface |
102
|
|
|
{ |
103
|
|
|
$resource['kind'] = 'AccessRole'; |
104
|
|
|
$resource = $this->resource_factory->validate($resource); |
105
|
|
|
|
106
|
|
|
if ($this->has($resource['name'])) { |
107
|
|
|
throw new Exception\NotUnique('access role '.$resource['name'].' does already exists'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->resource_factory->addTo($this->db->{self::COLLECTION_NAME}, $resource); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Update. |
115
|
|
|
*/ |
116
|
|
|
public function update(AccessRoleInterface $resource, array $data): bool |
117
|
|
|
{ |
118
|
|
|
$data['name'] = $resource->getName(); |
119
|
|
|
$data['kind'] = $resource->getKind(); |
120
|
|
|
$data = $this->resource_factory->validate($data); |
121
|
|
|
|
122
|
|
|
return $this->resource_factory->updateIn($this->db->{self::COLLECTION_NAME}, $resource, $data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Change stream. |
127
|
|
|
*/ |
128
|
|
|
public function watch(?ObjectIdInterface $after = null, bool $existing = true, ?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator |
129
|
|
|
{ |
130
|
|
|
$that = $this; |
131
|
|
|
|
132
|
|
|
return $this->resource_factory->watchFrom($this->db->{self::COLLECTION_NAME}, $after, $existing, $query, null, (function ($resource) use ($that) { |
133
|
|
|
return $that->build($resource); |
134
|
|
|
})->bindTo($this), $offset, $limit, $sort); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Build instance. |
139
|
|
|
*/ |
140
|
|
|
public function build(array $resource): AccessRoleInterface |
141
|
|
|
{ |
142
|
|
|
return $this->resource_factory->initResource(new AccessRole($resource)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.