Completed
Push — master ( 0929bc...18a624 )
by Igor
01:49
created

FileBind::setBind()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 4
crap 4
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 the file to the with owner
21
     *
22
     * @param Storage $storage
23
     * @param int $ownerId
24
     * @param int $ownerType
25
     * @param int $fileId
26
     * @return rkit\filemanager\models\File|bool
27
     */
28 18
    public function bindSingle($storage, $ownerId, $ownerType, $fileId)
29
    {
30 18
        $file = File::findOne($fileId);
31 18
        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...
32 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...
33 17
            if ($file->tmp) {
34 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...
35 17
                if ($file) {
36 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...
37 17
                    $file->updateAttributes($file->getDirtyAttributes());
38 17
                }
39 17
            }
40 17
            return $file;
41
        }
42
43 3
        return false;
44
    }
45
46
    /**
47
     * Bind files to the with owner
48
     *
49
     * @param Storage $storage
50
     * @param int $ownerId
51
     * @param int $ownerType
52
     * @param array $files
53
     * @return rkit\filemanager\models\File[]|bool
54
     */
55 9
    public function bindMultiple($storage, $ownerId, $ownerType, $files)
56
    {
57 9
        $newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id');
58
59 9
        if (count($newFiles)) {
60 9
            foreach ($newFiles as $fileId => $file) {
61 9
                if ($file->isOwner($ownerId, $ownerType)) {
62 9
                    $file->setStorage($storage);
63 9
                    if ($file->tmp) {
64 9
                        $file = $this->saveTmpDirToStorage($file, $ownerId);
65 9
                    }
66 9
                    if ($file) {
67 9
                        $file->position = @array_search($file->id, array_keys($files)) + 1;
68 9
                        $file->title = ArrayHelper::getValue($files, $file->id, $file->title);
69 9
                        $file->updateAttributes($file->getDirtyAttributes());
70 9
                        continue;
71
                    }
72 1
                }
73 2
                unset($newFiles[$fileId]);
74 2
                continue;
75 9
            }
76 9
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType, null, $newFiles);
77 9
        } else {
78 1
            $this->deleteCurrentFiles($storage, $ownerId, $ownerType);
79
        }
80
81 9
        return count($newFiles) ? $newFiles : false;
82
    }
83
84
    /**
85
     * Save temporary directory to the storage
86
     *
87
     * @param File $file
88
     * @param int $ownerId
89
     * @return rkit\filemanager\models\File|bool
90
     */
91 26
    private function saveTmpDirToStorage(File $file, $ownerId)
92
    {
93 26
        $file->owner_id = $ownerId;
94 26
        $file->tmp = false;
95 26
        if ($file->getStorage()->saveTmpDirToStorage()) {
96 26
            return $file;
97
        }
98
99 2
        return false;
100
    }
101
102
    /**
103
     * Delete current files
104
     *
105
     * @param Storage $storage
106
     * @param int $ownerId
107
     * @param int $ownerType
108
     * @param rkit\filemanager\models\File $exceptFile
109
     * @param rkit\filemanager\models\File[] $exceptFiles
110
     * @return void
111
     */
112 26
    private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFile = null, $exceptFiles = [])
113
    {
114 26
        $currentFiles = File::findAllByOwner($ownerId, $ownerType);
115 26
        foreach ($currentFiles as $currFile) {
116 11
            $isExceptFile = $exceptFile !== null && $currFile->id === $exceptFile->id;
117 11
            $isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles);
118 11
            if (!$isExceptFile && !$isExceptFiles) {
119 6
                $currFile->setStorage($storage);
120 6
                $currFile->delete();
121 6
            }
122 26
        }
123 26
    }
124
}
125