1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Bluz PHP Team |
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Application\Media; |
10
|
|
|
|
11
|
|
|
use Application\Users; |
12
|
|
|
use Bluz\Proxy\Auth; |
13
|
|
|
use Bluz\Validator\Traits\Validator; |
14
|
|
|
use Zend\Diactoros\UploadedFile; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Media Row |
18
|
|
|
* |
19
|
|
|
* @category Application |
20
|
|
|
* @package Media |
21
|
|
|
* |
22
|
|
|
* @property integer $id |
23
|
|
|
* @property integer $userId |
24
|
|
|
* @property string $title |
25
|
|
|
* @property string $module |
26
|
|
|
* @property string $type |
27
|
|
|
* @property string $file |
28
|
|
|
* @property string $thumb |
29
|
|
|
* @property integer $size |
30
|
|
|
* @property string $created |
31
|
|
|
* @property string $updated |
32
|
|
|
*/ |
33
|
|
|
class Row extends \Bluz\Db\Row |
34
|
|
|
{ |
35
|
|
|
use Validator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
protected function beforeSave() |
41
|
|
|
{ |
42
|
|
|
$this->addValidator('title') |
43
|
|
|
->required() |
44
|
|
|
->latinNumeric(' -_.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* __insert |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
protected function beforeInsert() |
53
|
|
|
{ |
54
|
|
|
$this->created = gmdate('Y-m-d H:i:s'); |
55
|
|
|
|
56
|
|
|
// set default module |
57
|
|
|
if (!$this->module) { |
58
|
|
|
$this->module = 'users'; |
59
|
|
|
} |
60
|
|
|
// set user ID |
61
|
|
|
if ($user = Auth::getIdentity()) { |
62
|
|
|
$this->userId = $user->id; |
63
|
|
|
} else { |
64
|
|
|
$this->userId = Users\Table::SYSTEM_USER; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* __update |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function beforeUpdate() |
74
|
|
|
{ |
75
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* postDelete |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function afterDelete() |
84
|
|
|
{ |
85
|
|
|
$this->deleteFiles(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* processRequestFile |
90
|
|
|
* |
91
|
|
|
* @param UploadedFile $file |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
* @throws \Bluz\Config\ConfigException |
95
|
|
|
*/ |
96
|
|
|
public function processRequestFile($file) |
97
|
|
|
{ |
98
|
|
|
// process request image |
99
|
|
|
$fileManager = new Manager($this, $file); |
100
|
|
|
|
101
|
|
|
// move request file |
102
|
|
|
$fileManager->moveToDir($this->userId.'/'.$this->module); |
103
|
|
|
|
104
|
|
|
// fill row data |
105
|
|
|
$this->title = $this->title ?: pathinfo($file->getClientFilename(), PATHINFO_FILENAME); |
106
|
|
|
$this->file = $fileManager->getPublicPath(); |
107
|
|
|
$this->type = $file->getClientMediaType(); |
108
|
|
|
$this->size = $file->getSize(); |
109
|
|
|
|
110
|
|
|
// create thumbnail |
111
|
|
|
$this->thumb = $fileManager->createThumbnail(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Delete Files |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function deleteFiles() |
120
|
|
|
{ |
121
|
|
View Code Duplication |
if ($this->file && is_file(PATH_PUBLIC.'/'.$this->file)) { |
|
|
|
|
122
|
|
|
@unlink(PATH_PUBLIC.'/'.$this->file); |
|
|
|
|
123
|
|
|
} |
124
|
|
View Code Duplication |
if ($this->thumb && is_file(PATH_PUBLIC.'/'.$this->thumb)) { |
|
|
|
|
125
|
|
|
@unlink(PATH_PUBLIC.'/'.$this->thumb); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.