LoadFileTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 37
ccs 4
cts 8
cp 0.5
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseFileAttributes() 0 15 4
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use yii\web\UploadedFile;
6
7
/**
8
 * Trait to configure and load uploaded files to a model.
9
 * @author Angel (Faryshta) Guevara <[email protected]>
10
 */
11
trait LoadFileTrait
12
{
13
    /**
14
     * @var string[] that defines which attributes will be recibe files.
15
     *
16
     * Example
17
     * ```php
18
     * [
19
     *     // 'avatar' attribute load file uploaded as 'avatar'
20
     *     'avatar',
21
     *     // 'background_fullsize' attribute load file uploaded as 'background'
22
     *     'background_fullsize' => 'background',
23
     * ]
24
     * ```
25
     */
26
    public array $fileAttributes = [];
27
28
    /**
29
     * Parse the allowed uploaded files.
30
     *
31
     * @return UploadedFile[] files sent to the action.
32
     */
33 5
    protected function parseFileAttributes(): array
34
    {
35 5
        $files = [];
36 5
        foreach ($this->fileAttributes as $attribute => $value) {
37
            if (is_int($attribute)) {
38
                $attribute = $value;
39
            }
40
            if (null !== ($uploadedFile = UploadedFile::getInstanceByName(
41
                $value
42
            ))) {
43
                $files[$attribute] = $uploadedFile;
44
            }
45
        }
46
47 5
        return $files;
48
    }
49
}
50