Completed
Push — master ( f15f2a...6d9f7a )
by Igor
02:25
created

FileBind::bindMultiple()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 22
cts 22
cp 1
rs 6.7272
cc 7
eloc 19
nc 4
nop 4
crap 7
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/filemanager-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\filemanager\behaviors;
10
11
use yii\helpers\ArrayHelper;
12
use rkit\filemanager\models\File;
13
14
/**
15
 * The bind class for FileBehavior
16
 */
17
class FileBind
18
{
19
    /**
20
     * Bind the file to the with owner
21
     * @internal
22
     *
23
     * @param Storage $storage The storage for the file
24
     * @param int $ownerId The id of the owner
25
     * @param int $ownerType The type of the owner
26
     * @param int $fileId The id of the file
27
     * @return rkit\filemanager\models\File|bool
28
     */
29 20
    public function bindSingle($storage, $ownerId, $ownerType, $fileId)
30
    {
31 20
        $file = File::findOne($fileId);
32 20
        if ($file && $file->isOwner($ownerId, $ownerType)) {
0 ignored issues
show
Bug introduced by
The method isOwner cannot be called on $file (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33 16
            $file->setStorage($storage);
0 ignored issues
show
Bug introduced by
The method setStorage cannot be called on $file (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
34 16
            if ($file->tmp) {
35 16
                $file = $this->saveTmpDirToStorage($file, $ownerId);
0 ignored issues
show
Documentation introduced by
$file is of type array|boolean, but the function expects a object<rkit\filemanager\models\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36 16
                if ($file) {
37 16
                    $this->deleteCurrentFiles($storage, $ownerId, $ownerType, [$file->id => $file]);
0 ignored issues
show
Documentation introduced by
array($file->id => $file) is of type array<integer,object<rki...lemanager\models\File>>, but the function expects a array<integer,object<rki...lemanager\models\File>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38 16
                    $file->updateAttributes($file->getDirtyAttributes());
39 16
                    $file->setStorage($storage);
40 16
                }
41 16
            }
42 16
            return $file;
43
        }
44
45 6
        return false;
46
    }
47
48
    /**
49
     * Bind files to the with owner
50
     *
51
     * @param Storage $storage The storage for the files
52
     * @param int $ownerId The id of the owner
53
     * @param int $ownerType The type of the owner
54
     * @param array $files Array of ids
55
     * @return rkit\filemanager\models\File[]|bool
56
     * @SuppressWarnings(PHPMD.ElseExpression)
57
     */
58 3
    public function bindMultiple($storage, $ownerId, $ownerType, $files)
59
    {
60 3
        $newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id');
61
62 3
        if (count($newFiles)) {
63 3
            foreach ($newFiles as $fileId => $file) {
64 3
                if ($file->isOwner($ownerId, $ownerType)) {
65 3
                    $file->setStorage($storage);
66 3
                    if ($file->tmp) {
67 3
                        $file = $this->saveTmpDirToStorage($file, $ownerId);
68 3
                    }
69 3
                    if ($file) {
70 3
                        $file->position = @array_search($file->id, array_keys($files)) + 1;
71 3
                        $file->title = ArrayHelper::getValue($files, $file->id, $file->title);
72 3
                        $file->updateAttributes($file->getDirtyAttributes());
73 3
                        continue;
74
                    }
75 1
                }
76 1
                unset($newFiles[$fileId]);
77 1
                continue;
78 3
            }
79 3
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType, $newFiles);
80 3
        } else {
81 1
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType);
82
        }
83
84 3
        return count($newFiles) ? $newFiles : false;
85
    }
86
87
    /**
88
     * Save the temporary file to the storage
89
     *
90
     * @param File $file
91
     * @param int $ownerId The id of the owner
92
     * @return rkit\filemanager\models\File|bool
93
     */
94 19
    private function saveTmpDirToStorage(File $file, $ownerId)
95
    {
96 19
        $file->owner_id = $ownerId;
97 19
        $file->tmp = false;
98 19
        if ($file->getStorage()->saveTemporaryFileToStorage()) {
99 19
            return $file;
100
        }
101
102 2
        return false;
103
    }
104
105
    /**
106
     * Delete current files
107
     *
108
     * @param Storage $storage
109
     * @param int $ownerId The id of the owner
110
     * @param int $ownerType The type of the owner
111
     * @param rkit\filemanager\models\File[] $exceptFiles
112
     * @return void
113
     */
114 19
    private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFiles = [])
115
    {
116 19
        $currentFiles = File::findAllByOwner($ownerId, $ownerType);
117 19
        foreach ($currentFiles as $currFile) {
118 5
            $isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles);
119 5
            if (!$isExceptFiles) {
120 4
                $currFile->setStorage($storage);
121 4
                $currFile->delete();
122 4
            }
123 19
        }
124 19
    }
125
}
126