Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Factory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 109
ccs 25
cts 32
cp 0.7813
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 4 1
A getAll() 0 4 1
A getOne() 0 14 2
A update() 0 8 1
A deleteOne() 0 6 1
A add() 0 10 2
A watch() 0 4 1
A build() 0 4 1
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([
67 4
            'name' => $name,
68
        ], [
69 4
            'projection' => ['history' => 0],
70
        ]);
71
72 4
        if ($result === null) {
73 2
            throw new Exception\NotFound('namespace '.$name.' is not registered');
74
        }
75
76 2
        return $this->build($result);
77
    }
78
79
    /**
80
     * Update.
81
     */
82
    public function update(ResourceNamespaceInterface $resource, array $data): bool
83
    {
84
        $data['name'] = $resource->getName();
85
        $data['kind'] = $resource->getKind();
86
        $data = Validator::validate($data);
87
88
        return $this->updateIn($this->db->{self::COLLECTION_NAME}, $resource, $data);
89
    }
90
91
    /**
92
     * Delete by name.
93
     */
94 2
    public function deleteOne(string $name): bool
95
    {
96 2
        $resource = $this->getOne($name);
97
98 1
        return $this->deleteFrom($this->db->{self::COLLECTION_NAME}, $resource->getId());
99
    }
100
101
    /**
102
     * Add namespace.
103
     */
104 8
    public function add(array $resource): ObjectIdInterface
105
    {
106 8
        Validator::validate($resource);
107
108 8
        if ($this->has($resource['name'])) {
109 1
            throw new Exception\NotUnique('namespace '.$resource['name'].' does already exists');
110
        }
111
112 8
        return $this->addTo($this->db->{self::COLLECTION_NAME}, $resource);
113
    }
114
115
    /**
116
     * Change stream.
117
     */
118
    public function watch(?ObjectIdInterface $after = null, bool $existing = true, ?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator
119
    {
120
        return $this->watchFrom($this->db->{self::COLLECTION_NAME}, $after, $existing, $query, null, $offset, $limit, $sort);
121
    }
122
123
    /**
124
     * Build instance.
125
     */
126 5
    public function build(array $resource): ResourceNamespaceInterface
127
    {
128 5
        return $this->initResource(new ResourceNamespace($resource['name'], $this, $this->collection_factory, $resource));
129
    }
130
}
131