Completed
Push — master ( 519f76...318dd5 )
by Raffael
24:13 queued 15:29
created

Collection::countObjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 2
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;
13
14
use Generator;
15
use MongoDB\BSON\ObjectIdInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Log\LoggerInterface;
18
use Tubee\Collection\CollectionInterface;
19
use Tubee\DataObject\DataObjectInterface;
20
use Tubee\DataObject\Factory as DataObjectFactory;
21
use Tubee\Endpoint\EndpointInterface;
22
use Tubee\Endpoint\Factory as EndpointFactory;
23
use Tubee\Resource\AbstractResource;
24
use Tubee\Resource\AttributeResolver;
25
use Tubee\ResourceNamespace\ResourceNamespaceInterface;
26
use Tubee\Schema\SchemaInterface;
27
28
class Collection extends AbstractResource implements CollectionInterface
29
{
30
    /**
31
     * Kind.
32
     */
33
    public const KIND = 'Collection';
34
35
    /**
36
     * Collection name.
37
     *
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * ResourceNamespace.
44
     *
45
     * @var ResourceNamespaceInterface
46
     */
47
    protected $namespace;
48
49
    /**
50
     * Schema.
51
     *
52
     * @var SchemaInterface
53
     */
54
    protected $schema;
55
56
    /**
57
     * Logger.
58
     *
59
     * @var LoggerInterface
60
     */
61
    protected $logger;
62
63
    /**
64
     * Dataobject factory.
65
     *
66
     * @var DataObjectFactory
67
     */
68
    protected $object_factory;
69
70
    /**
71
     * Endpoint factory.
72
     *
73
     * @var EndpointFactory
74
     */
75
    protected $endpoint_factory;
76
77
    /**
78
     * Collection name.
79
     *
80
     * @var string
81
     */
82
    protected $collection;
83
84
    /**
85
     * Initialize.
86
     */
87 11
    public function __construct(string $name, ResourceNamespaceInterface $namespace, EndpointFactory $endpoint_factory, DataObjectFactory $object_factory, SchemaInterface $schema, LoggerInterface $logger, array $resource = [])
88
    {
89 11
        $this->resource = $resource;
90 11
        $this->name = $name;
91 11
        $this->collection = 'objects'.'.'.$namespace->getName().'.'.$name;
92 11
        $this->namespace = $namespace;
93 11
        $this->schema = $schema;
94 11
        $this->endpoint_factory = $endpoint_factory;
95 11
        $this->logger = $logger;
96 11
        $this->object_factory = $object_factory;
97 11
    }
98
99
    /**
100
     * Get collection.
101
     */
102 7
    public function getCollection(): string
103
    {
104 7
        return $this->collection;
105
    }
106
107
    /**
108
     * Get schema.
109
     */
110
    public function getSchema(): SchemaInterface
111
    {
112
        return $this->schema;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 2
    public function getResourceNamespace(): ResourceNamespaceInterface
119
    {
120 2
        return $this->namespace;
121
    }
122
123
    /**
124
     * Decorate.
125
     */
126 1
    public function decorate(ServerRequestInterface $request): array
127
    {
128
        $resource = [
129
            '_links' => [
130 1
                'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$this->getResourceNamespace()->getName())],
131
            ],
132 1
            'kind' => 'Collection',
133 1
            'namespace' => $this->namespace->getName(),
134
            'data' => [
135 1
                'schema' => $this->schema->getSchema(),
136
            ],
137
       ];
138
139 1
        return AttributeResolver::resolve($request, $this, $resource);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function hasEndpoint(string $name): bool
146
    {
147
        return $this->endpoint_factory->has($this, $name);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getEndpoint(string $name): EndpointInterface
154
    {
155
        return $this->endpoint_factory->getOne($this, $name);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getEndpoints(array $endpoints = [], ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator
162
    {
163
        return $this->endpoint_factory->getAll($this, $endpoints, $offset, $limit, $sort);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getSourceEndpoints(array $endpoints = [], ?int $offset = null, ?int $limit = null): Generator
170
    {
171
        $query = ['data.type' => EndpointInterface::TYPE_SOURCE];
172
        if ($endpoints !== []) {
173
            $query = ['$and' => [$query, $endpoints]];
174
        }
175
176
        return $this->endpoint_factory->getAll($this, $query, $offset, $limit);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getDestinationEndpoints(array $endpoints = [], ?int $offset = null, ?int $limit = null): Generator
183
    {
184
        $query = ['data.type' => EndpointInterface::TYPE_DESTINATION];
185
        if ($endpoints !== []) {
186
            $query = ['$and' => [$query, $endpoints]];
187
        }
188
189
        return $this->endpoint_factory->getAll($this, $query, $offset, $limit);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 1
    public function getIdentifier(): string
196
    {
197 1
        return $this->namespace->getIdentifier().'::'.$this->name;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function getObjectHistory(ObjectIdInterface $id, ?array $filter = null, ?int $offset = null, ?int $limit = null): Generator
204
    {
205
        return $this->object_factory->getObjectHistory($this, $id, $filter, $offset, $limit);
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getObject(array $filter, bool $include_dataset = true, int $version = 0): DataObjectInterface
212
    {
213
        return $this->object_factory->getOne($this, $filter, $include_dataset, $version);
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function getObjects(array $query = [], bool $include_dataset = true, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator
220
    {
221
        return $this->object_factory->getAll($this, $query, $include_dataset, $offset, $limit, $sort);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function countObjects(array $query = [], bool $include_dataset = true, ?int $offset = null, ?int $limit = null, ?array $sort = null): int
0 ignored issues
show
Unused Code introduced by
The parameter $include_dataset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sort is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
    {
229
        return $this->object_factory->count($this, $query);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 1
    public function createObject(array $object, bool $simulate = false, ?array $endpoints = null): ObjectIdInterface
236
    {
237 1
        return $this->object_factory->create($this, $object, $simulate, $endpoints);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function changeObject(DataObjectInterface $object, array $data, bool $simulate = false, ?array $endpoints = null): bool
244
    {
245
        return $this->object_factory->update($this, $object, $data, $simulate, $endpoints);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function deleteObject(ObjectIdInterface $id, bool $simulate = false): bool
252
    {
253
        return $this->object_factory->deleteOne($this, $id, $simulate);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function flush(bool $simulate = false): bool
260
    {
261
        return $this->object_factory->deleteAll($this, $simulate);
262
    }
263
}
264