Passed
Push — master ( 7ba2cb...1d30f9 )
by Peter
02:20
created

AdminResourceSqlDataMapper::getByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Orm\DataMappers;
6
7
use AbterPhp\Admin\Domain\Entities\AdminResource as Entity;
8
use Opulence\Orm\DataMappers\SqlDataMapper;
9
use Opulence\QueryBuilders\MySql\QueryBuilder;
10
use Opulence\QueryBuilders\MySql\SelectQuery;
11
12
class AdminResourceSqlDataMapper extends SqlDataMapper implements IAdminResourceDataMapper
13
{
14
    /**
15
     * @param Entity $entity
16
     */
17
    public function add($entity)
18
    {
19
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Admin\Domain\Entities\AdminResource.
Loading history...
20
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects an AdminResource entity.');
21
        }
22
23
        $query = (new QueryBuilder())
24
            ->insert(
25
                'admin_resources',
26
                [
27
                    'id'         => $entity->getId(),
28
                    'identifier' => $entity->getIdentifier(),
29
                ]
30
            );
31
32
        $statement = $this->writeConnection->prepare($query->getSql());
33
        $statement->bindValues($query->getParameters());
34
        $statement->execute();
35
    }
36
37
    /**
38
     * @param Entity $entity
39
     */
40
    public function delete($entity)
41
    {
42
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Admin\Domain\Entities\AdminResource.
Loading history...
43
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects an AdminResource entity.');
44
        }
45
46
        $query = (new QueryBuilder())
47
            ->update('admin_resources', 'admin_resources', ['deleted' => [1, \PDO::PARAM_INT]])
48
            ->where('id = ?')
49
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
50
51
        $statement = $this->writeConnection->prepare($query->getSql());
52
        $statement->bindValues($query->getParameters());
53
        $statement->execute();
54
    }
55
56
    /**
57
     * @return Entity[]
58
     */
59
    public function getAll(): array
60
    {
61
        $query = $this->getBaseQuery();
62
63
        $sql = $query->getSql();
64
65
        return $this->read($sql, [], self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
68
    /**
69
     * @param int|string $id
70
     *
71
     * @return Entity|null
72
     */
73
    public function getById($id)
74
    {
75
        $query = $this->getBaseQuery()->andWhere('ar.id = :admin_resource_id');
76
77
        $parameters = [
78
            'admin_resource_id' => [$id, \PDO::PARAM_STR],
79
        ];
80
81
        return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ENTITY, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...ALUE_TYPE_ENTITY, true) also could return the type array which is incompatible with the documented return type AbterPhp\Admin\Domain\Entities\AdminResource|null.
Loading history...
82
    }
83
84
    /**
85
     * @param string $identifier
86
     *
87
     * @return Entity|null
88
     */
89
    public function getByIdentifier(string $identifier): ?Entity
90
    {
91
        $query = $this->getBaseQuery()->andWhere('ar.identifier = :identifier');
92
93
        $parameters = [
94
            'identifier' => [$identifier, \PDO::PARAM_STR],
95
        ];
96
97
        return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ENTITY, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...ALUE_TYPE_ENTITY, true) could return the type array which is incompatible with the type-hinted return AbterPhp\Admin\Domain\Entities\AdminResource|null. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
100
    /**
101
     * @param string $userId
102
     *
103
     * @return Entity[]
104
     */
105
    public function getByUserId(string $userId): array
106
    {
107
        $query = $this->getBaseQuery()
108
            ->innerJoin('user_groups_admin_resources', 'ugar', 'ugar.admin_resource_id = ar.id')
109
            ->innerJoin('user_groups', 'ug', 'ug.id = ugar.user_group_id')
110
            ->innerJoin('users_user_groups', 'uug', 'uug.user_group_id = ug.id')
111
            ->andWhere('uug.user_id = :user_id')
112
            ->groupBy('ar.id');
113
114
        $sql    = $query->getSql();
115
        $params = [
116
            'user_id' => [$userId, \PDO::PARAM_STR],
117
        ];
118
119
        return $this->read($sql, $params, self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
120
    }
121
122
    /**
123
     * @param Entity $entity
124
     */
125
    public function update($entity)
126
    {
127
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Admin\Domain\Entities\AdminResource.
Loading history...
128
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects an AdminResource entity.');
129
        }
130
131
        $query = (new QueryBuilder())
132
            ->update(
133
                'admin_resources',
134
                'admin_resources',
135
                [
136
                    'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
137
                ]
138
            )
139
            ->where('id = ?')
140
            ->andWhere('deleted = 0')
141
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
142
143
        $statement = $this->writeConnection->prepare($query->getSql());
144
        $statement->bindValues($query->getParameters());
145
        $statement->execute();
146
    }
147
148
    /**
149
     * @param array $hash
150
     *
151
     * @return Entity
152
     */
153
    protected function loadEntity(array $hash)
154
    {
155
        return new Entity(
156
            $hash['id'],
157
            $hash['identifier']
158
        );
159
    }
160
161
    /**
162
     * @return SelectQuery
163
     */
164
    private function getBaseQuery()
165
    {
166
        /** @var SelectQuery $query */
167
        $query = (new QueryBuilder())
168
            ->select(
169
                'ar.id',
170
                'ar.identifier'
171
            )
172
            ->from('admin_resources', 'ar')
173
            ->where('ar.deleted = 0');
174
175
        return $query;
176
    }
177
}
178