AbstractDaoService::getDao()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Db;
22
23
use Labrys\Getter;
24
25
/**
26
 * Abstract DAO-backed Service
27
 */
28
abstract class AbstractDaoService implements EntityRepo
29
{
30
    /**
31
     * @var \Minotaur\Acl\Gatekeeper
32
     */
33
    protected $gatekeeper;
34
    /**
35
     * @var string
36
     */
37
    protected $readPermission;
38
39
    /**
40
     * Creates a new AbstractDaoService.
41
     *
42
     * @param $gatekeeper - The security gatekeeper
43
     * @param $readPermission - The ACL permission for read access
44
     */
45
    public function __construct(
46
        \Minotaur\Acl\Gatekeeper $gatekeeper,
47
        string $readPermission = 'read'
48
    ) {
49
        $this->gatekeeper = $gatekeeper;
50
        $this->readPermission = $readPermission;
51
    }
52
53
    /**
54
     * Gets the DAO.
55
     *
56
     * @return - The backing DAO
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
57
     */
58
    abstract protected function getDao(): EntityRepo;
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getType(): string
64
    {
65
        return $this->getDao()->getType();
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function countAll(array $criteria): int
72
    {
73
        return $this->getDao()->countAll($criteria);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function findOne(array $criteria)
80
    {
81
        return $this->getDao()->findOne($criteria);
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function findAll(array $criteria, \Caridea\Http\Pagination $pagination = null, bool $totalCount = false): iterable
88
    {
89
        return $this->getDao()->findAll($criteria, $pagination, $totalCount);
90
    }
91
92
    /**
93
     * @inheritDoc
94
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
95
     */
96
    public function findById($id)
97
    {
98
        $dao = $this->getDao();
99
        $entity = $dao->findById($id);
100
        if ($entity !== null) {
101
            $this->gatekeeper->assert($this->readPermission, $dao->getType(), $id);
102
        }
103
        return $entity;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
109
     */
110
    public function get($id)
111
    {
112
        return $this->getAndAssert($id, $this->readPermission);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
118
     */
119
    public function getAll(iterable $ids): iterable
120
    {
121
        $dao = $this->getDao();
122
        $all = $dao->getAll($ids);
123
        $this->gatekeeper->assertAll($this->readPermission, $dao->getType(), $ids);
124
        return $all;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getInstanceMap(iterable $entities): array
131
    {
132
        return $this->getDao()->getInstanceMap($entities);
133
    }
134
135
    /**
136
     * Gets the entity and asserts an ACL permission.
137
     *
138
     * @param $id - The entity id
139
     * @param $verb - The verb (e.g. 'read', 'write')
140
     * @return - The entity
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
141
     * @throws \Caridea\Dao\Exception\Unreachable If the connection fails
142
     * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist
143
     * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs
144
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
145
     */
146
    protected function getAndAssert($id, string $verb)
147
    {
148
        $dao = $this->getDao();
149
        $entity = $dao->get($id);
150
        $this->gatekeeper->assert($verb, $dao->getType(), $id);
151
        return $entity;
152
    }
153
}
154