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