ImageService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A init() 0 3 1
A getImageByPath() 0 4 2
A get() 0 4 1
A getInstance() 0 6 2
A __toString() 0 3 1
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
}