Passed
Push — master ( c1f9a4...4b0053 )
by Rimas
02:42 queued 32s
created

FileFieldsTrait::getFileFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
namespace Dokobit\Gateway\Query\File;
3
4
trait FileFieldsTrait
5
{
6
    /**
7
     * Get file fields for API query
8
     * @param string $path file path
9
     * @return array
10
     */
11
    public function getFileFields($path)
12
    {
13
        if (!file_exists($path)) {
14
            throw new \RuntimeException(sprintf('File "%s" does not exist', $path));
15
        }
16
17
        $content = file_get_contents($path);
18
19
        return [
20
            'name' => basename($path),
21
            'digest' => sha1($content),
22
            'content' => base64_encode($content)
23
        ];
24
    }
25
26
    /**
27
     * Get multiple file fileds for API query by parsing list of file paths
28
     * @param array $files file paths
29
     * @return array
30
     */
31
    public function getMultipleFileFields(array $files)
32
    {
33
        $return = [];
34
35
        foreach ($files as $path) {
36
            $return[] = $this->getFileFields($path);
37
        }
38
39
        return $return;
40
    }
41
}
42