Passed
Push — master ( 3896ee...e50616 )
by Raffael
05:45
created

Factory::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 = $this->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
        $resource['kind'] = 'Namespace';
107 8
        $resource = $this->validate($resource);
108
109 8
        if ($this->has($resource['name'])) {
110 1
            throw new Exception\NotUnique('namespace '.$resource['name'].' does already exists');
111
        }
112
113 8
        return $this->addTo($this->db->{self::COLLECTION_NAME}, $resource);
114
    }
115
116
    /**
117
     * Change stream.
118
     */
119
    public function watch(?ObjectIdInterface $after = null, bool $existing = true, ?array $query = null, ?int $offset = null, ?int $limit = null, ?array $sort = null): Generator
120
    {
121
        return $this->watchFrom($this->db->{self::COLLECTION_NAME}, $after, $existing, $query, null, $offset, $limit, $sort);
122
    }
123
124
    /**
125
     * Build instance.
126
     */
127 5
    public function build(array $resource): ResourceNamespaceInterface
128
    {
129 5
        return $this->initResource(new ResourceNamespace($resource['name'], $this, $this->collection_factory, $resource));
130
    }
131
}
132