Completed
Pull Request — master (#255)
by Anton
05:56
created

Row::beforeSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
crap 1
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\Proxy\Auth;
14
use Bluz\Validator\Traits\Validator;
15
use Bluz\Validator\Validator as v;
16
use Image\Thumbnail;
17
18
/**
19
 * Media Row
20
 *
21
 * @category Application
22
 * @package  Media
23
 *
24
 * @property integer $id
25
 * @property integer $userId
26
 * @property string $title
27
 * @property string $module
28
 * @property string $type
29
 * @property string $file
30
 * @property string $preview
31
 * @property integer $size
32
 * @property string $created
33
 * @property string $updated
34
 */
35
class Row extends \Bluz\Db\Row
36
{
37
    use Validator;
38
39
    const THUMB_HEIGHT = 196;
40
    const THUMB_WIDTH = 196;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function beforeSave()
46
    {
47 1
        $this->addValidator(
48 1
            'title',
49 1
            v::required()->latinNumeric(' -_.')
50
        );
51 1
    }
52
53
    /**
54
     * __insert
55
     *
56
     * @return void
57
     */
58 1
    protected function beforeInsert()
59
    {
60 1
        $this->created = gmdate('Y-m-d H:i:s');
61
62
        // set default module
63 1
        if (!$this->module) {
64 1
            $this->module = 'users';
65
        }
66
        // set user ID
67 1 View Code Duplication
        if ($user = Auth::getIdentity()) {
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...
68 1
            $this->userId = $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Bluz\Auth\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
        } else {
70
            $this->userId = Users\Table::SYSTEM_USER;
71
        }
72
73
        // create preview
74
        // set full path
75 1
        $image = new Thumbnail(PATH_PUBLIC .'/'. $this->file);
76 1
        $image->setHeight(self::THUMB_HEIGHT);
77 1
        $image->setWidth(self::THUMB_WIDTH);
78 1
        $preview = $image->generate();
79
        // crop full path
80 1
        $preview = substr($preview, strlen(PATH_PUBLIC) + 1);
81 1
        $this->preview = $preview;
82 1
    }
83
84
    /**
85
     * __update
86
     *
87
     * @return void
88
     */
89
    protected function beforeUpdate()
90
    {
91
        $this->updated = gmdate('Y-m-d H:i:s');
92
    }
93
94
    /**
95
     * postDelete
96
     *
97
     * @return void
98
     */
99
    protected function afterDelete()
100
    {
101 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...
102
            @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...
103
        }
104 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...
105
            @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...
106
        }
107
    }
108
}
109