FileFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 8.06 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 5
loc 62
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromArray() 5 20 3
A createFromObject() 0 14 1
A getRealPath() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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