Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

AdminResourceSqlDataMapper::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 9.8666
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 AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
use Opulence\Orm\DataMappers\SqlDataMapper;
10
use Opulence\QueryBuilders\Expression;
11
use Opulence\QueryBuilders\MySql\QueryBuilder;
12
use Opulence\QueryBuilders\MySql\SelectQuery;
13
14
/** @phan-file-suppress PhanTypeMismatchArgument */
15
class AdminResourceSqlDataMapper extends SqlDataMapper implements IAdminResourceDataMapper
16
{
17
    /**
18
     * @param IStringerEntity $entity
19
     */
20
    public function add($entity)
21
    {
22
        assert($entity instanceof Entity, new \InvalidArgumentException());
23
24
        $query = (new QueryBuilder())
25
            ->insert(
26
                'admin_resources',
27
                [
28
                    'id'         => $entity->getId(),
29
                    'identifier' => $entity->getIdentifier(),
30
                ]
31
            );
32
33
        $statement = $this->writeConnection->prepare($query->getSql());
34
        $statement->bindValues($query->getParameters());
35
        $statement->execute();
36
    }
37
38
    /**
39
     * @param IStringerEntity $entity
40
     *
41
     * @throws \Opulence\QueryBuilders\InvalidQueryException
42
     */
43
    public function delete($entity)
44
    {
45
        assert($entity instanceof Entity, new \InvalidArgumentException());
46
47
        $query = (new QueryBuilder())
48
            ->update(
49
                'admin_resources',
50
                'admin_resources',
51
                ['deleted_at' => new Expression('NOW()')]
52
            )
53
            ->where('id = ?')
54
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
55
56
        $statement = $this->writeConnection->prepare($query->getSql());
57
        $statement->bindValues($query->getParameters());
58
        $statement->execute();
59
    }
60
61
    /**
62
     * @return Entity[]
63
     * @throws \Opulence\Orm\OrmException
64
     */
65
    public function getAll(): array
66
    {
67
        $query = $this->getBaseQuery();
68
69
        $sql = $query->getSql();
70
71
        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...
72
    }
73
74
    /**
75
     * @param int|string $id
76
     *
77
     * @return Entity|null
78
     * @throws \Opulence\Orm\OrmException
79
     */
80
    public function getById($id)
81
    {
82
        $query = $this->getBaseQuery()->andWhere('ar.id = :admin_resource_id');
83
84
        $parameters = [
85
            'admin_resource_id' => [$id, \PDO::PARAM_STR],
86
        ];
87
88
        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...
89
    }
90
91
    /**
92
     * @param string $identifier
93
     *
94
     * @return Entity|null
95
     * @throws \Opulence\Orm\OrmException
96
     */
97
    public function getByIdentifier(string $identifier): ?Entity
98
    {
99
        $query = $this->getBaseQuery()->andWhere('ar.identifier = :identifier');
100
101
        $parameters = [
102
            'identifier' => [$identifier, \PDO::PARAM_STR],
103
        ];
104
105
        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...
106
    }
107
108
    /**
109
     * @param string $userId
110
     *
111
     * @return Entity[]
112
     * @throws \Opulence\Orm\OrmException
113
     */
114
    public function getByUserId(string $userId): array
115
    {
116
        $query = $this->getBaseQuery()
117
            ->innerJoin('user_groups_admin_resources', 'ugar', 'ugar.admin_resource_id = ar.id')
118
            ->innerJoin('user_groups', 'ug', 'ug.id = ugar.user_group_id')
119
            ->innerJoin('users_user_groups', 'uug', 'uug.user_group_id = ug.id')
120
            ->andWhere('uug.user_id = :user_id')
121
            ->groupBy('ar.id');
122
123
        $sql    = $query->getSql();
124
        $params = [
125
            'user_id' => [$userId, \PDO::PARAM_STR],
126
        ];
127
128
        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...
129
    }
130
131
    /**
132
     * @param IStringerEntity $entity
133
     *
134
     * @throws \Opulence\QueryBuilders\InvalidQueryException
135
     */
136
    public function update($entity)
137
    {
138
        assert($entity instanceof Entity, new \InvalidArgumentException());
139
140
        $query = (new QueryBuilder())
141
            ->update(
142
                'admin_resources',
143
                'admin_resources',
144
                [
145
                    'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
146
                ]
147
            )
148
            ->where('id = ?')
149
            ->andWhere('deleted_at IS NULL')
150
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
151
152
        $statement = $this->writeConnection->prepare($query->getSql());
153
        $statement->bindValues($query->getParameters());
154
        $statement->execute();
155
    }
156
157
    /**
158
     * @param array $hash
159
     *
160
     * @return Entity
161
     */
162
    protected function loadEntity(array $hash): Entity
163
    {
164
        return new Entity(
165
            $hash['id'],
166
            $hash['identifier']
167
        );
168
    }
169
170
    /**
171
     * @return SelectQuery
172
     */
173
    private function getBaseQuery()
174
    {
175
        /** @var SelectQuery $query */
176
        $query = (new QueryBuilder())
177
            ->select(
178
                'ar.id',
179
                'ar.identifier'
180
            )
181
            ->from('admin_resources', 'ar')
182
            ->where('ar.deleted_at IS NULL');
183
184
        return $query;
185
    }
186
}
187