Completed
Pull Request — master (#247)
by
unknown
09:38
created

Row::beforeInsert()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 5
Ratio 20 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 4
nop 0
dl 5
loc 25
rs 8.8571
ccs 13
cts 14
cp 0.9286
crap 3.0032
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Media;
11
12
use Application\Users;
13
use Bluz\Validator\Traits\Validator;
14
use Bluz\Validator\Validator as v;
15
use Image\Thumbnail;
16
17
/**
18
 * Media Row
19
 *
20
 * @category Application
21
 * @package  Media
22
 *
23
 * @property integer $id
24
 * @property integer $userId
25
 * @property string $title
26
 * @property string $module
27
 * @property string $type
28
 * @property string $file
29
 * @property string $preview
30
 * @property string $created
31
 * @property string $updated
32
 */
33
class Row extends \Bluz\Db\Row
34
{
35
    use Validator;
36
37
    const THUMB_HEIGHT = 120;
38
    const THUMB_WIDTH = 120;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    protected function beforeSave()
44
    {
45 1
        $this->addValidator(
46 1
            'title',
47 1
            v::required()->latinNumeric(' -_')
48
        );
49 1
    }
50
51
    /**
52
     * __insert
53
     *
54
     * @return void
55
     */
56 1
    protected function beforeInsert()
57
    {
58 1
        $this->created = gmdate('Y-m-d H:i:s');
59
60
        // set default module
61 1
        if (!$this->module) {
62 1
            $this->module = 'users';
63
        }
64
        // set user ID
65 1 View Code Duplication
        if ($user = app()->user()) {
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...
66 1
            $this->userId = $user->id;
67
        } else {
68
            $this->userId = Users\Table::SYSTEM_USER;
69
        }
70
71
        // create preview
72
        // set full path
73 1
        $image = new Thumbnail(PATH_PUBLIC .'/'. $this->file);
74 1
        $image->setHeight(self::THUMB_HEIGHT);
75 1
        $image->setWidth(self::THUMB_WIDTH);
76 1
        $preview = $image->generate();
77
        // crop full path
78 1
        $preview = substr($preview, strlen(PATH_PUBLIC) + 1);
79 1
        $this->preview = $preview;
80 1
    }
81
82
    /**
83
     * __update
84
     *
85
     * @return void
86
     */
87
    protected function beforeUpdate()
88
    {
89
        $this->updated = gmdate('Y-m-d H:i:s');
90
    }
91
92
    /**
93
     * postDelete
94
     *
95
     * @return void
96
     */
97
    protected function afterDelete()
98
    {
99 View Code Duplication
        if (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...
100
            @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...
101
        }
102 View Code Duplication
        if (is_file(PATH_PUBLIC .'/'. $this->preview)) {
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...
103
            @unlink(PATH_PUBLIC .'/'. $this->preview);
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...
104
        }
105
    }
106
}
107