FilesFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace Kambo\Http\Message\Factories\Environment\Superglobal;
3
4
// \Http\Message
5
use Kambo\Http\Message\UploadedFile;
6
use Kambo\Http\Message\Environment\Environment;
7
use Kambo\Http\Message\Factories\Environment\Interfaces\FactoryInterface;
8
9
/**
10
 * Create instances of UploadedFile object from instance of Environment object
11
 *
12
 * @package Kambo\Http\Message\Factories\Environment\Superglobal
13
 * @author  Bohuslav Simek <[email protected]>
14
 * @license MIT
15
 */
16
class FilesFactory implements FactoryInterface
17
{
18
    /**
19
     * Create instances of UploadedFile object from instance of Environment object
20
     *
21
     * @param Environment $environment environment data
22
     *
23
     * @return array An associative array containing instances of UploadedFile, if there 
24
     *               are no uploads an empty array will be returned:
25
     *               [<field name> => <instance of UploadedFile>, ...]
26
     */
27 5
    public function create(Environment $environment)
28
    {
29 5
        $files  = $environment->getFiles();
30 5
        $parsed = $this->parseFiles($files);
31
32 5
        return $parsed;
33
    }
34
35
    /**
36
     * Parse files data into instances of UploadedFile
37
     *
38
     * @param array $files An associative array of items with same structure as $_FILES 
39
     *
40
     * @return array An associative array containing instances of UploadedFile, if there 
41
     *               are no uploads an empty array will be returned:
42
     *               [<field name> => <instance of UploadedFile>, ...]           
43
     */
44 5
    private function parseFiles($files)
45
    {
46 5
        $parsed = [];
47 5
        if (!empty($files)) {
48 2
            foreach ($files as $field => $file) {
49 2
                $parsed[$field] = $this->parseFile($file);
50 2
            }
51 2
        }
52
53 5
        return $parsed;
54
    }
55
56
    /**
57
     * Parse particular fields in files data into instances of UploadedFile
58
     *
59
     * @param array $uploadedFile An associative array of items from $_FILES from one single field
60
     *
61
     * @return array array with instances of UploadedFile
62
     */
63 2
    private function parseFile($uploadedFile)
64
    {
65 2
        $item = [];
66
67 2
        if (is_array($uploadedFile)) {
68 2
            if (!is_array($uploadedFile['error'])) {
69 1
                $item[] = new UploadedFile(
70 1
                    $uploadedFile['tmp_name'],
71 1
                    isset($uploadedFile['name']) ? $uploadedFile['name'] : null,
72 1
                    isset($uploadedFile['type']) ? $uploadedFile['type'] : null,
73 1
                    isset($uploadedFile['size']) ? $uploadedFile['size'] : null,
74 1
                    $uploadedFile['error']
75 1
                );
76 1
            } else {
77 1
                foreach ($uploadedFile['error'] as $fieldIndex => $void) {
78 1
                    $item[] = new UploadedFile(
79 1
                        $uploadedFile['tmp_name'][$fieldIndex],
80 1
                        isset($uploadedFile['name']) ? $uploadedFile['name'][$fieldIndex] : null,
81 1
                        isset($uploadedFile['type']) ? $uploadedFile['type'][$fieldIndex] : null,
82 1
                        isset($uploadedFile['size']) ? $uploadedFile['size'][$fieldIndex] : null,
83 1
                        $uploadedFile['error'][$fieldIndex]
84 1
                    );
85 1
                }
86
            }
87 2
        }
88
89 2
        return $item;
90
    }
91
}
92