FilesTransformer::getFilesCollectionFromArrays()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace ArgentCrusade\Selectel\CloudStorage\Traits;
4
5
use ArgentCrusade\Selectel\CloudStorage\File;
6
use ArgentCrusade\Selectel\CloudStorage\Collections\Collection;
7
8
trait FilesTransformer
9
{
10
    /**
11
     * Container name. This name will be used in transformation process.
12
     *
13
     * @return string
14
     */
15
    abstract public function containerName();
16
17
    /**
18
     * API Client.
19
     *
20
     * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract
21
     */
22
    abstract public function apiClient();
23
24
    /**
25
     * Transforms file array to instance of File object.
26
     *
27
     * @param array $file File array.
28
     *
29
     * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract
30
     */
31
    public function getFileFromArray(array $file)
32
    {
33
        return new File($this->apiClient(), $this->containerName(), $file);
34
    }
35
36
    /**
37
     * Transforms Collection of file arrays (or plain array) to Collection of File objects.
38
     * Warning: converting a lot of files to `File` instances may result in performance loss.
39
     *
40
     * @param array|\ArgentCrusade\Selectel\CloudStorage\Collections\Collection $files
41
     *
42
     * @return \ArgentCrusade\Selectel\CloudStorage\Collections\Collection
43
     */
44
    public function getFilesCollectionFromArrays($files)
45
    {
46
        $collection = new Collection();
47
48
        foreach ($files as $file) {
49
            $collection[] = $this->getFileFromArray($file);
50
        }
51
52
        return $collection;
53
    }
54
}
55