FileFactory::getRealPath()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
2
3
namespace WSW\SimpleUpload\Factories\Entities;
4
5
use InvalidArgumentException;
6
use WSW\SimpleUpload\Entities\AbstractEntity;
7
use WSW\SimpleUpload\Entities\File;
8
use WSW\SimpleUpload\Factories\AbstractFactory;
9
use WSW\SimpleUpload\Services\SimpleUpload;
10
use WSW\SimpleUpload\Services\Translator;
11
12
/**
13
 * Class FileFactory
14
 * @package WSW\SimpleUpload\Factories\Entities
15
 */
16
abstract class FileFactory extends AbstractFactory
17
{
18
    /**
19
     * @param array $data
20 4
     * @param Translator|null $translator
21
     * @return \WSW\SimpleUpload\Entities\File
22 4
     */
23
    public static function createFromArray(array $data, Translator $translator = null)
24 4
    {
25 4
        $translator = $translator ?: Translator::locate();
26
        $arr = ['path' => null, 'timestamp' => null, 'size' => null, 'mimetype' => null];
27
        $compare = array_diff_key($arr, $data);
28 4
29 4 View Code Duplication
        if (!empty($compare)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            $fields = array_keys($compare);
31
            $msg = sprintf($translator->getMessage('validations.requiredField'), $fields[0]);
32 4
            throw new InvalidArgumentException($msg, 400);
33 4
        }
34
35
        $entity = new File();
36 4
        $entity->setPath($data['path']);
37 4
        $entity->setTimestamp($data['timestamp']);
38
        $entity->setSize($data['size']);
39
        $entity->setMimetype($data['mimetype']);
40 4
41
        return $entity;
42
    }
43
44
    /**
45
     * @param SimpleUpload $SimpleUpload
46
     * @return File
47 3
     */
48
    public static function createFromObject(SimpleUpload $SimpleUpload)
49
    {
50 3
        $arr = [
51 3
            'path' => self::getRealPath(
52 3
                $SimpleUpload->getFileSystem()->getAdapter()->getPathPrefix(),
53
                $SimpleUpload->getNewNameFile()
54 3
            ),
55 3
            'timestamp' => (int) $SimpleUpload->getFileSystem()->getTimestamp($SimpleUpload->getNewNameFile()),
56 3
            'size'      => (int) $SimpleUpload->getFileSystem()->getSize($SimpleUpload->getNewNameFile()),
57
            'mimetype'  => $SimpleUpload->getFileSystem()->getMimetype($SimpleUpload->getNewNameFile())
58
        ];
59 3
60
        return self::createFromArray($arr, $SimpleUpload->getTranslator());
61
    }
62
63
    /**
64
     * @param null $path
65
     * @param string $file
66
     * @return string
67 4
     */
68
    public static function getRealPath($path = null, $file = null)
69 4
    {
70 1
        if (null === $path) {
71
            $path = DIRECTORY_SEPARATOR;
72
        }
73 4
74 4
        $realPath = ($path{0} !== DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR . $path : $path;
75
        return $realPath . $file;
76
    }
77
}
78