Completed
Push — master ( 342ffa...2ca792 )
by Andrii
05:25
created

File::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
ccs 0
cts 7
cp 0
crap 2
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
/**
13
 * Created by PhpStorm.
14
 * User: tofid
15
 * Date: 11.02.15
16
 * Time: 17:59.
17
 */
18
19
namespace hipanel\behaviors;
20
21
use hipanel\models\File as FileModel;
22
use yii\base\Behavior;
23
use yii\db\BaseActiveRecord;
24
use yii\helpers\FileHelper;
25
use yii\web\UploadedFile;
26
27
class File extends Behavior
28
{
29
    /**
30
     * @var string the attribute which holds the attachment.
31
     */
32
    public $attribute = 'file';
33
34
    /**
35
     * @var array the scenarios in which the behavior will be triggered
36
     */
37
    public $scenarios = [];
38
39
    /**
40
     * @var string the attribute that will receive the file id
41
     */
42
    public $savedAttribute;
43
44
    /**
45
     * @var UploadedFile the uploaded file instance.
46
     */
47
    private $_file;
48
49
    /**
50
     * @return array
51
     */
52
    public function events()
53
    {
54
        return [
55
            BaseActiveRecord::EVENT_BEFORE_INSERT => 'saveUploadedFile',
56
            BaseActiveRecord::EVENT_BEFORE_UPDATE => 'saveUploadedFile',
57
        ];
58
    }
59
60
    /**
61
     * Event handler for beforeSave.
62
     * @param \yii\base\ModelEven|boolean $event
63
     */
64
    public function saveUploadedFile($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        $model = $this->owner;
67
        $arr_ids = [];
0 ignored issues
show
Unused Code introduced by
$arr_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
        if (in_array($model->scenario, $this->scenarios, true)) {
69
            $this->_file = UploadedFile::getInstances($model, $this->attribute);
0 ignored issues
show
Compatibility introduced by
$model of type object<yii\base\Component> is not a sub-type of object<yii\base\Model>. It seems like you assume a child class of the class yii\base\Component to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Documentation Bug introduced by
It seems like \yii\web\UploadedFile::g...odel, $this->attribute) of type array<integer,object<yii\web\UploadedFile>> is incompatible with the declared type object<yii\web\UploadedFile> of property $_file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
            if (is_array($this->_file) && !empty($this->_file)) {
71
                $this->owner->{$this->savedAttribute} = implode(',', FileModel::fileSave($this->_file));
72
//                foreach ($this->_file as $file) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
//                    if ($file instanceof UploadedFile) {
74
//                        // Move to temporary destination
75
//                        $tempDestination = FileModel::getTempFolder() . DIRECTORY_SEPARATOR . uniqid() . '.' . $file->extension;
76
//                        FileHelper::createDirectory(dirname($tempDestination));
77
//                        $file->saveAs($tempDestination);
78
//                        // Prepare to final destination
79
//                        $url = FileModel::getTmpUrl(basename($tempDestination));
80
//                        $response =  FileModel::perform('Put', [
81
//                            'url' => $url,
82
//                            'filename' => basename($tempDestination)
83
//                        ]);
84
//
85
//                        $file_id = $arr_ids[] = $response['id'];
86
//                        $finalDestination = $this->getPath($file_id) . DIRECTORY_SEPARATOR . $file_id;
87
//                        FileHelper::createDirectory(dirname($finalDestination));
88
//                        if (!rename($tempDestination, $finalDestination))
89
//                            throw new \LogicException('rename function is not work');
90
//                        if (is_file($tempDestination))
91
//                            unlink($tempDestination);
92
//                    }
93
//                }
94
//                $this->owner->{$this->savedAttribute} = implode(',', $arr_ids);
95
            } else {
96
                // Protect attribute
97
                unset($model->{$this->attribute});
98
            }
99
        }
100
    }
101
}
102