Passed
Push — master ( b91050...6fb96f )
by Jens
02:59
created

FileService::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 5
loc 5
rs 9.4285
1
<?php
2
/**
3
 * Created by jensk on 12-10-2017.
4
 */
5
6
namespace CloudControl\Cms\services;
7
use CloudControl\Cms\storage\entities\File;
8
use CloudControl\Cms\storage\Storage;
9
10
/**
11
 * Class FileService
12
 * Singleton
13
 *
14
 * @package CloudControl\Cms\services
15
 */
16 View Code Duplication
class FileService
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
17
{
18
    private static $instance;
19
    /**
20
     * @var Storage
21
     */
22
    protected $storage;
23
24
    /**
25
     * FileService constructor.
26
     */
27
    protected function __construct()
28
    {}
29
30
    /**
31
     * @return FileService
32
     */
33
    public static function getInstance()
34
    {
35
        if (!self::$instance instanceof FileService) {
36
            self::$instance = new FileService();
37
        }
38
        return self::$instance;
39
    }
40
41
    /**
42
     * @param $filePath
43
     * @return File
44
     */
45
    public static function get($filePath)
46
    {
47
        $instance = self::getInstance();
48
        return $instance->getFileByPath($filePath);
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function __toString()
55
    {
56
        return print_r(self::$instance, true);
57
    }
58
59
    /**
60
     * @param $filePath
61
     * @return File
62
     */
63
    protected function getFileByPath($filePath)
64
    {
65
        $file = $this->storage->getFiles()->getFileByName($filePath);
66
        return new File($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->storage->getFiles...etFileByName($filePath) on line 65 can be null; however, CloudControl\Cms\storage...ies\File::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
    }
68
69
    /**
70
     * @param Storage $storage
71
     */
72
    public function init(Storage $storage)
73
    {
74
        $this->storage = $storage;
75
    }
76
77
78
}