Completed
Push — master ( 2804eb...4748ae )
by Ronaldo
02:28
created

UploadFactory::createFromArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 2
crap 3
1
<?php
2
3
namespace WSW\SimpleUpload\Factories\Entities;
4
5
use InvalidArgumentException;
6
use WSW\SimpleUpload\Entities\Upload;
7
use WSW\SimpleUpload\Factories\AbstractFactory;
8
use WSW\SimpleUpload\Services\Translator;
9
10
/**
11
 * Class UploadFactory
12
 * @package WSW\SimpleUpload\Factories\Entities
13
 */
14
abstract class UploadFactory extends AbstractFactory
15
{
16
17
    /**
18
     * @param array $data
19
     * @param Translator|null $translator
20
     * @return Upload
21
     */
22 9
    public static function createFromArray(array $data, Translator $translator = null)
23
    {
24 9
        $translator  = $translator ?: Translator::locate();
25 9
        $arr = ['name' => null, 'type' => null, 'tmp_name' => null, 'error' => null, 'size' => null];
26
27 9
        $compare = array_diff_key($arr, $data);
28
29 9
        if (!empty($compare)) {
30 5
            $fields = array_keys($compare);
31 5
            $msg = sprintf($translator->getMessage('validations.requiredField'), $fields[0]);
32 5
            throw new InvalidArgumentException($msg, 400);
33
        }
34
35 4
        $entity = (new Upload())
36 4
            ->setName($data['name'])
37 4
            ->setType($data['type'])
38 4
            ->setTmpName($data['tmp_name'])
39 4
            ->setError($data['error'])
40 4
            ->setSize($data['size']);
41
42 4
        return $entity;
43
    }
44
}
45