ImageService::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 12-10-2017.
4
 */
5
6
namespace CloudControl\Cms\services;
7
8
9
use CloudControl\Cms\storage\entities\Image;
10
use CloudControl\Cms\storage\Storage;
11
12
/**
13
 * Class ImageService
14
 * Singleton
15
 * @package CloudControl\Cms\services
16
 */
17
class ImageService
18
{
19
    private static $instance;
20
    /**
21
     * @var Storage
22
     */
23
    protected $storage;
24
25
    /**
26
     * ImageService constructor.
27
     */
28
    protected function __construct()
29
    {
30
    }
31
32
    /**
33
     * @return ImageService
34
     */
35
    public static function getInstance()
36
    {
37
        if (!self::$instance instanceof ImageService) {
38
            self::$instance = new ImageService();
39
        }
40
        return self::$instance;
41
    }
42
43
    /**
44
     * @param $imagePath
45
     * @return Image
46
     */
47
    public static function get($imagePath)
48
    {
49
        $instance = self::getInstance();
50
        return $instance->getImageByPath($imagePath);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function __toString()
57
    {
58
        return (string)print_r(self::$instance, true);
59
    }
60
61
    /**
62
     * @param $imagePath
63
     * @return Image|null
64
     */
65
    protected function getImageByPath($imagePath)
66
    {
67
        $image = $this->storage->getImages()->getImageByName($imagePath);
68
        return $image === null ? null : new Image($image);
69
    }
70
71
    /**
72
     * @param Storage $storage
73
     */
74
    public function init(Storage $storage)
75
    {
76
        $this->storage = $storage;
77
    }
78
}