Passed
Push — master ( 57b2d8...55b64c )
by Raffael
22:23 queued 13s
created

Factory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 133
ccs 30
cts 40
cp 0.75
rs 10
c 0
b 0
f 0

9 Methods

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