FileDownloadSqlDataMapper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 105
c 1
b 0
f 0
dl 0
loc 239
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseQuery() 0 28 1
A getByUserId() 0 8 1
A getAll() 0 5 1
A getPage() 0 23 4
A getByFileId() 0 9 1
A delete() 0 19 1
A add() 0 21 1
A getById() 0 9 1
A loadEntity() 0 19 1
A update() 0 23 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Orm\DataMappers;
6
7
use AbterPhp\Admin\Domain\Entities\User;
8
use AbterPhp\Admin\Domain\Entities\UserLanguage;
9
use AbterPhp\Files\Domain\Entities\File;
10
use AbterPhp\Files\Domain\Entities\FileDownload as Entity;
11
use Opulence\Orm\DataMappers\SqlDataMapper;
12
use Opulence\QueryBuilders\Expression;
13
use Opulence\QueryBuilders\MySql\QueryBuilder;
14
use Opulence\QueryBuilders\MySql\SelectQuery;
15
16
/** @phan-file-suppress PhanTypeMismatchArgument */
17
class FileDownloadSqlDataMapper extends SqlDataMapper implements IFileDownloadDataMapper
18
{
19
    /**
20
     * @param Entity $entity
21
     */
22
    public function add($entity)
23
    {
24
        assert($entity instanceof Entity, new \InvalidArgumentException());
25
26
        $query = (new QueryBuilder())
27
            ->insert(
28
                'file_downloads',
29
                [
30
                    'id'            => [$entity->getId(), \PDO::PARAM_STR],
31
                    'file_id'       => [$entity->getFile()->getId(), \PDO::PARAM_STR],
32
                    'user_id'       => [$entity->getUser()->getId(), \PDO::PARAM_STR],
33
                    'downloaded_at' => [$entity->getDownloadedAt()->format(Entity::DATE_FORMAT), \PDO::PARAM_STR],
34
                ]
35
            );
36
37
        $sql    = $query->getSql();
38
        $params = $query->getParameters();
39
40
        $statement = $this->writeConnection->prepare($sql);
41
        $statement->bindValues($params);
42
        $statement->execute();
43
    }
44
45
    /**
46
     * @param Entity $entity
47
     *
48
     * @throws \Opulence\QueryBuilders\InvalidQueryException
49
     */
50
    public function delete($entity)
51
    {
52
        assert($entity instanceof Entity, new \InvalidArgumentException());
53
54
        $query = (new QueryBuilder())
55
            ->update(
56
                'file_downloads',
57
                'file_downloads',
58
                ['deleted_at' => new Expression('NOW()')]
59
            )
60
            ->where('id = ?')
61
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
62
63
        $sql    = $query->getSql();
64
        $params = $query->getParameters();
65
66
        $statement = $this->writeConnection->prepare($sql);
67
        $statement->bindValues($params);
68
        $statement->execute();
69
    }
70
71
    /**
72
     * @return Entity[]
73
     * @throws \Opulence\Orm\OrmException
74
     */
75
    public function getAll(): array
76
    {
77
        $query = $this->getBaseQuery();
78
79
        return $this->read($query->getSql(), [], self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...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...
80
    }
81
82
    /**
83
     * @param int      $limitFrom
84
     * @param int      $pageSize
85
     * @param string[] $orders
86
     * @param array    $conditions
87
     * @param array    $params
88
     *
89
     * @return Entity[]
90
     * @throws \Opulence\Orm\OrmException
91
     */
92
    public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array
93
    {
94
        $query = $this->getBaseQuery()
95
            ->limit($pageSize)
96
            ->offset($limitFrom);
97
98
        if (!$orders) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orders of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99
            $query->orderBy('downloaded_at DESC');
100
        }
101
        foreach ($orders as $order) {
102
            $query->addOrderBy($order);
103
        }
104
105
        foreach ($conditions as $condition) {
106
            $query->andWhere($condition);
107
        }
108
109
        $replaceCount = 1;
110
111
        $sql = $query->getSql();
112
        $sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount);
113
114
        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...
115
    }
116
117
    /**
118
     * @param int|string $id
119
     *
120
     * @return Entity
121
     * @throws \Opulence\Orm\OrmException
122
     */
123
    public function getById($id)
124
    {
125
        $query = $this->getBaseQuery()->andWhere('file_downloads.id = :file_download_id');
126
127
        $parameters = [
128
            'file_download_id' => [$id, \PDO::PARAM_STR],
129
        ];
130
131
        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\Files\Domain\Entities\FileDownload.
Loading history...
132
    }
133
134
    /**
135
     * @param string $userId
136
     *
137
     * @return Entity[]
138
     * @throws \Opulence\Orm\OrmException
139
     */
140
    public function getByUserId(string $userId): array
141
    {
142
        $query      = $this->getBaseQuery()->andWhere('user_id = :user_id');
143
        $parameters = [
144
            'user_id' => [$userId, \PDO::PARAM_STR],
145
        ];
146
147
        return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...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...
148
    }
149
150
    /**
151
     * @param string $fileId
152
     *
153
     * @return Entity[]
154
     * @throws \Opulence\Orm\OrmException
155
     */
156
    public function getByFileId(string $fileId): array
157
    {
158
        $query = $this->getBaseQuery()->andWhere('file_id = :file_id');
159
160
        $parameters = [
161
            'file_id' => [$fileId, \PDO::PARAM_STR],
162
        ];
163
164
        return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($quer...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...
165
    }
166
167
    /**
168
     * @param object $entity
169
     *
170
     * @throws \Opulence\QueryBuilders\InvalidQueryException
171
     */
172
    public function update($entity)
173
    {
174
        assert($entity instanceof Entity, new \InvalidArgumentException());
175
176
        $query = (new QueryBuilder())
177
            ->update(
178
                'file_downloads',
179
                'file_downloads',
180
                [
181
                    'file_id'       => [$entity->getFile()->getId(), \PDO::PARAM_STR],
182
                    'user_id'       => [$entity->getUser()->getId(), \PDO::PARAM_STR],
183
                    'downloaded_at' => [$entity->getDownloadedAt()->format(Entity::DATE_FORMAT), \PDO::PARAM_STR],
184
                ]
185
            )
186
            ->where('id = ?')
187
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
188
189
        $sql    = $query->getSql();
190
        $params = $query->getParameters();
191
192
        $statement = $this->writeConnection->prepare($sql);
193
        $statement->bindValues($params);
194
        $statement->execute();
195
    }
196
197
    /**
198
     * @param array $hash
199
     *
200
     * @return Entity
201
     * @throws \Exception
202
     */
203
    protected function loadEntity(array $hash)
204
    {
205
        $file         = new File($hash['file_id'], $hash['filesystem_name'], $hash['public_name'], $hash['mime'], '');
206
        $userLanguage = new UserLanguage('', '', '');
207
        $user         = new User(
208
            $hash['user_id'],
209
            $hash['username'],
210
            '',
211
            '',
212
            true,
213
            true,
214
            $userLanguage
215
        );
216
217
        return new Entity(
218
            $hash['id'],
219
            $file,
220
            $user,
221
            new \DateTime($hash['downloaded_at'])
222
        );
223
    }
224
225
    /**
226
     * @return SelectQuery
227
     */
228
    private function getBaseQuery()
229
    {
230
        /** @var SelectQuery $query */
231
        $query = (new QueryBuilder())
232
            ->select(
233
                'file_downloads.id',
234
                'file_downloads.file_id',
235
                'file_downloads.user_id',
236
                'file_downloads.downloaded_at',
237
                'files.filesystem_name AS filesystem_name',
238
                'files.public_name AS public_name',
239
                'files.mime AS mime',
240
                'users.username AS username'
241
            )
242
            ->from('file_downloads')
243
            ->innerJoin(
244
                'files',
245
                'files',
246
                'files.id=file_downloads.file_id'
247
            )
248
            ->innerJoin(
249
                'users',
250
                'users',
251
                'users.id=file_downloads.user_id'
252
            )
253
            ->where('file_downloads.deleted_at IS NULL');
254
255
        return $query;
256
    }
257
}
258