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

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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