Completed
Push — master ( 9b439e...de8363 )
by Igor
03:05
created

FileBind::bindMultiple()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 23
cts 23
cp 1
rs 6.7272
cc 7
eloc 20
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
 * This is the bind class for FileBehavior
16
 */
17
class FileBind
18
{
19
    /**
20
     * Bind files with owner
21
     *
22
     * @param Storage $storage
23
     * @param int $ownerId
24
     * @param int $ownerType
25
     * @param int|array $fileId
26
     * @return File|File[]|bool
27
     */
28 26
    public function setBind($storage, $ownerId, $ownerType, $fileId)
29
    {
30 26
        if ($fileId === [] || $fileId === '') {
31 3
            (new File())->deleteByOwner($storage, $ownerId, $ownerType);
32 3
            return true;
33
        }
34
35 26
        if (is_array($fileId)) {
36 9
            return $this->bindMultiple($storage, $ownerId, $ownerType, $fileId);
37
        } else {
38 17
            return $this->bindSingle($storage, $ownerId, $ownerType, $fileId);
39
        }
40
    }
41
42
    /**
43
     * Bind the file to the with owner
44
     *
45
     * @param Storage $storage
46
     * @param int $ownerId
47
     * @param int $ownerType
48
     * @param int $fileId
49
     * @return rkit\filemanager\models\File|bool
50
     */
51 17
    private function bindSingle($storage, $ownerId, $ownerType, $fileId)
52
    {
53 17
        $file = $fileId ? File::findOne($fileId) : false;
54
55 17
        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...
56 17
            $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...
57 17
            if ($file->tmp) {
58 17
                $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...
59 17
                if ($file) {
60 17
                    $this->deleteCurrentFiles($storage, $ownerId, $ownerType, $file);
0 ignored issues
show
Documentation introduced by
$file is of type object<rkit\filemanager\models\File>, but the function expects a object<rkit\filemanager\...nager\models\File>|null.

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...
61 17
                    $file->updateAttributes($file->getDirtyAttributes());
62 17
                }
63 17
            }
64 17
            return $file;
65
        }
66
67 2
        return false;
68
    }
69
70
    /**
71
     * Bind files to the with owner
72
     *
73
     * @param Storage $storage
74
     * @param int $ownerId
75
     * @param int $ownerType
76
     * @param array $files
77
     * @return rkit\filemanager\models\File[]|bool
78
     */
79 9
    private function bindMultiple($storage, $ownerId, $ownerType, $files)
80
    {
81 9
        $files = ArrayHelper::getValue($files, 'files', []);
82 9
        $newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id');
83
84 9
        if (count($newFiles)) {
85 9
            foreach ($newFiles as $fileId => $file) {
86 9
                if ($file->isOwner($ownerId, $ownerType)) {
87 9
                    $file->setStorage($storage);
88 9
                    if ($file->tmp) {
89 9
                        $file = $this->saveTmpDirToStorage($file, $ownerId);
90 9
                    }
91 9
                    if ($file) {
92 9
                        $file->position = @array_search($file->id, array_keys($files)) + 1;
93 9
                        $file->title = ArrayHelper::getValue($files, $file->id, $file->title);
94 9
                        $file->updateAttributes($file->getDirtyAttributes());
95 9
                        continue;
96
                    }
97 1
                }
98 2
                unset($newFiles[$fileId]);
99 2
                continue;
100 9
            }
101 9
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType, null, $newFiles);
102 9
        } else {
103 1
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType);
104
        }
105
106 9
        return count($newFiles) ? $newFiles : false;
107
    }
108
109
    /**
110
     * Save temporary directory to the storage
111
     *
112
     * @param rkit\filemanager\models\File $file
113
     * @param int $ownerId
114
     * @return rkit\filemanager\models\File|bool
115
     */
116 26
    private function saveTmpDirToStorage(File $file, $ownerId)
117
    {
118 26
        $file->owner_id = $ownerId;
119 26
        $file->tmp = false;
120 26
        if ($file->getStorage()->saveTmpDirToStorage()) {
121 26
            return $file;
122
        }
123
124 2
        return false;
125
    }
126
127
    /**
128
     * Delete current files
129
     *
130
     * @param Storage $storage
131
     * @param int $ownerId
132
     * @param int $ownerType
133
     * @param rkit\filemanager\models\File $exceptFile
134
     * @param rkit\filemanager\models\File[] $exceptFiles
135
     * @return void
136
     */
137 26
    private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFile = null, $exceptFiles = [])
138
    {
139 26
        $currentFiles = File::findAllByOwner($ownerId, $ownerType);
140 26
        foreach ($currentFiles as $currFile) {
141 11
            $isExceptFile = $exceptFile !== null && $currFile->id === $exceptFile->id;
142 11
            $isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles);
143 11
            if (!$isExceptFile && !$isExceptFiles) {
144 6
                $currFile->setStorage($storage);
145 6
                $currFile->delete();
146 6
            }
147 26
        }
148 26
    }
149
}
150