|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* tubee.io |
|
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\ResourceNamespace; |
|
13
|
|
|
|
|
14
|
|
|
use Generator; |
|
15
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
|
16
|
|
|
use MongoDB\Database; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Tubee\Collection\Factory as CollectionFactory; |
|
19
|
|
|
use Tubee\Resource\Factory as ResourceFactory; |
|
20
|
|
|
use Tubee\ResourceNamespace; |
|
21
|
|
|
|
|
22
|
|
|
class Factory extends ResourceFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Collection name. |
|
26
|
|
|
*/ |
|
27
|
|
|
public const COLLECTION_NAME = 'namespaces'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Datatype. |
|
31
|
|
|
* |
|
32
|
|
|
* @var CollectionFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $collection_factory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialize. |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function __construct(Database $db, CollectionFactory $collection_factory, LoggerInterface $logger) |
|
40
|
|
|
{ |
|
41
|
11 |
|
$this->collection_factory = $collection_factory; |
|
42
|
11 |
|
parent::__construct($db, $logger); |
|
43
|
11 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Has namespace. |
|
47
|
|
|
*/ |
|
48
|
9 |
|
public function has(string $name): bool |
|
49
|
|
|
{ |
|
50
|
9 |
|
return $this->db->{self::COLLECTION_NAME}->count(['name' => $name]) > 0; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get all. |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function getAll(?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator |
|
57
|
|
|
{ |
|
58
|
3 |
|
return $this->getAllFrom($this->db->{self::COLLECTION_NAME}, $query, $offset, $limit, $sort); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get namespace. |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public function getOne(string $name): ResourceNamespaceInterface |
|
65
|
|
|
{ |
|
66
|
4 |
|
$result = $this->db->{self::COLLECTION_NAME}->findOne(['name' => $name]); |
|
67
|
|
|
|
|
68
|
4 |
|
if ($result === null) { |
|
69
|
2 |
|
throw new Exception\NotFound('namespace '.$name.' is not registered'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
return $this->build($result); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Update. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function update(ResourceNamespaceInterface $resource, array $data): bool |
|
79
|
|
|
{ |
|
80
|
|
|
$data['name'] = $resource->getName(); |
|
81
|
|
|
$data['kind'] = $resource->getKind(); |
|
82
|
|
|
$data = Validator::validate($data); |
|
83
|
|
|
|
|
84
|
|
|
return $this->updateIn($this->db->{self::COLLECTION_NAME}, $resource, $data); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Delete by name. |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function deleteOne(string $name): bool |
|
91
|
|
|
{ |
|
92
|
2 |
|
$resource = $this->getOne($name); |
|
93
|
|
|
|
|
94
|
1 |
|
return $this->deleteFrom($this->db->{self::COLLECTION_NAME}, $resource->getId()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add namespace. |
|
99
|
|
|
*/ |
|
100
|
8 |
|
public function add(array $resource): ObjectIdInterface |
|
101
|
|
|
{ |
|
102
|
8 |
|
Validator::validate($resource); |
|
103
|
|
|
|
|
104
|
8 |
|
if ($this->has($resource['name'])) { |
|
105
|
1 |
|
throw new Exception\NotUnique('namespace '.$resource['name'].' does already exists'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
8 |
|
return $this->addTo($this->db->{self::COLLECTION_NAME}, $resource); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Change stream. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function watch(?ObjectIdInterface $after = null, bool $existing = true, ?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->watchFrom($this->db->{self::COLLECTION_NAME}, $after, $existing, $query, null, $offset, $limit, $sort); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Build instance. |
|
121
|
|
|
*/ |
|
122
|
5 |
|
public function build(array $resource): ResourceNamespaceInterface |
|
123
|
|
|
{ |
|
124
|
5 |
|
return $this->initResource(new ResourceNamespace($resource['name'], $this, $this->collection_factory, $resource)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|