Passed
Push — develop ( fc59e1...90305f )
by Jens
02:30
created

Image::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * Created by jensk on 12-10-2017.
4
 */
5
6
namespace CloudControl\Cms\services\imageservice;
7
8
9
use CloudControl\Cms\cc\Request;
10
11
/**
12
 * Class Image
13
 * @package CloudControl\Cms\services\imageservice
14
 */
15
class Image
16
{
17
    protected $imagePath;
18
    public $file;
19
    public $type;
20
    public $size;
21
    public $set;
22
23
    /**
24
     * Image constructor.
25
     * @param \stdClass $image
26
     * @param string $imagePath
27
     */
28
    public function __construct(\stdClass $image, $imagePath = 'images')
29
    {
30
        $this->file = $image->file;
31
        $this->type = $image->type;
32
        $this->size = $image->size;
33
        $this->set  = $image->set;
34
        $this->imagePath = $imagePath;
35
    }
36
37
    /**
38
     * @param string $imageVariant
39
     * @return string
40
     * @throws \Exception
41
     */
42
    public function get($imageVariant = 'original')
43
    {
44
        if (!isset($this->set->{$imageVariant})) {
45
            throw new \Exception('Image variant `' . $imageVariant . '` does not exist. Existing variants are ' . implode(', ', array_keys((array) $this->set)));
46
        }
47
        return Request::$subfolders . $this->imagePath . '/' . $this->set->{$imageVariant};
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function __toString()
54
    {
55
        return Request::$subfolders . $this->imagePath . '/' . $this->file;
56
    }
57
}