Completed
Pull Request — master (#8)
by Anton
02:06
created

Row::afterDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     */
95
    public function processRequestFile($file)
96
    {
97
        // process request image
98
        $fileManager = new Manager($this, $file);
99
100
        // move request file
101
        $fileManager->moveToDir($this->userId .'/'. $this->module);
102
103
        // fill row data
104
        $this->title = $this->title ?: pathinfo($file->getClientFilename(), PATHINFO_FILENAME);
105
        $this->file = $fileManager->getPublicPath();
106
        $this->type = $file->getClientMediaType();
107
        $this->size = $file->getSize();
108
109
        // create thumbnail
110
        $this->thumb = $fileManager->createThumbnail();
111
    }
112
113
    /**
114
     * Delete Files
115
     *
116
     * @return void
117
     */
118
    public function deleteFiles()
119
    {
120 View Code Duplication
        if ($this->file && is_file(PATH_PUBLIC.'/'.$this->file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
121
            @unlink(PATH_PUBLIC.'/'.$this->file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
        }
123 View Code Duplication
        if ($this->thumb && is_file(PATH_PUBLIC.'/'.$this->thumb)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
124
            @unlink(PATH_PUBLIC.'/'.$this->thumb);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
125
        }
126
    }
127
}
128