Completed
Pull Request — master (#250)
by Anton
13:39
created

Row::afterDelete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 6
Ratio 66.67 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 4
nop 0
dl 6
loc 9
ccs 0
cts 8
cp 0
crap 12
rs 9.6666
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 string $created
32
 * @property string $updated
33
 */
34
class Row extends \Bluz\Db\Row
35
{
36
    use Validator;
37
38
    const THUMB_HEIGHT = 120;
39
    const THUMB_WIDTH = 120;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    protected function beforeSave()
45
    {
46 1
        $this->addValidator(
47 1
            'title',
48 1
            v::required()->latinNumeric(' -_')
49 1
        );
50 1
    }
51
52
    /**
53
     * __insert
54
     *
55
     * @return void
56
     */
57 1
    protected function beforeInsert()
58
    {
59 1
        $this->created = gmdate('Y-m-d H:i:s');
60
61
        // set default module
62 1
        if (!$this->module) {
63 1
            $this->module = 'users';
64 1
        }
65
        // set user ID
66 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...
67 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...
68 1
        } else {
69
            $this->userId = Users\Table::SYSTEM_USER;
70
        }
71
72
        // create preview
73
        // set full path
74 1
        $image = new Thumbnail(PATH_PUBLIC .'/'. $this->file);
75 1
        $image->setHeight(self::THUMB_HEIGHT);
76 1
        $image->setWidth(self::THUMB_WIDTH);
77 1
        $preview = $image->generate();
78
        // crop full path
79 1
        $preview = substr($preview, strlen(PATH_PUBLIC) + 1);
80 1
        $this->preview = $preview;
81 1
    }
82
83
    /**
84
     * __update
85
     *
86
     * @return void
87
     */
88
    protected function beforeUpdate()
89
    {
90
        $this->updated = gmdate('Y-m-d H:i:s');
91
    }
92
93
    /**
94
     * postDelete
95
     *
96
     * @return void
97
     */
98
    protected function afterDelete()
99
    {
100 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...
101
            @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...
102
        }
103 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...
104
            @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...
105
        }
106
    }
107
}
108