ResourceImageDO   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 145
ccs 48
cts 48
cp 1
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 9 1
A setType() 0 4 1
A getWidth() 0 4 1
A setWidth() 0 7 1
A getHeight() 0 4 1
A setHeight() 0 7 1
A setFilePath() 0 4 1
A getDirectoryTokens() 0 7 1
A getDimension() 0 10 3
A getCrop() 0 4 1
A setCrop() 0 5 1
A toArray() 0 12 2
1
<?php
2
namespace Staticus\Resources\Image;
3
use Staticus\Resources\ResourceDOAbstract;
4
5
/**
6
 * Domain Object
7
 * @package Staticus\Resources\File
8
 */
9
abstract class ResourceImageDO extends ResourceDOAbstract implements ResourceImageDOInterface
10
{
11
    const DEFAULT_WIDTH = 0;
12
    const DEFAULT_HEIGHT = 0;
13
    const DEFAULT_DIMENSION = 0;
14
    const TOKEN_DIMENSION = 'dimension';
15
16
    protected $width = 0;
17
    protected $height = 0;
18
19
    /**
20
     * @var CropImageDOInterface
21
     */
22
    protected $crop;
23
24 13
    public function reset()
25
    {
26 13
        parent::reset();
27 13
        $this->type = static::TYPE;
28 13
        $this->width = 0;
29 13
        $this->height = 0;
30 13
        $this->crop = null;
31 13
        return $this;
32
    }
33
34
    /**
35
     * You can't change the concrete ImageType
36
     * @param string $type
37
     * @return $this
38
     */
39 5
    public function setType($type)
40
    {
41 5
        return $this;
42
    }
43
44
    /**
45
     * @return int
46
     */
47 10
    public function getWidth()
48
    {
49 10
        return $this->width;
50
    }
51
52
    /**
53
     * @param integer $width
54
     * @return ResourceImageDO
55
     */
56 6
    public function setWidth($width = self::DEFAULT_WIDTH)
57
    {
58 6
        $this->width = (int)$width;
59 6
        $this->setFilePath();
60
61 6
        return $this;
62
    }
63
64
    /**
65
     * @return int
66
     */
67 10
    public function getHeight()
68
    {
69 10
        return $this->height;
70
    }
71
72
    /**
73
     * @param integer $height
74
     * @return ResourceImageDO
75
     */
76 6
    public function setHeight($height = self::DEFAULT_HEIGHT)
77
    {
78 6
        $this->height = (int)$height;
79 6
        $this->setFilePath();
80
81 6
        return $this;
82
    }
83
84 8
    protected function setFilePath()
85
    {
86 8
        $this->filePath = $this->generateFilePath();
87 8
    }
88
89
    /**
90
     * Map of the resource directory elements.
91
     * For example, you can use it with the strtok() method. Or for routes buildings.
92
     *
93
     * @return array
94
     * @see strtok()
95
     * @example strtok($relative_path, '/');
96
     */
97 9
    public function getDirectoryTokens()
98
    {
99 9
        $tokens = parent::getDirectoryTokens();
100 9
        $tokens[static::TOKEN_DIMENSION] = $this->getDimension() . DIRECTORY_SEPARATOR;
101
102 9
        return $tokens;
103
    }
104
105
    /**
106
     * @return int|string
107
     */
108 10
    public function getDimension()
109
    {
110 10
        $width = $this->getWidth();
111 10
        $height = $this->getHeight();
112 10
        $size = ($width > 0 && $height > 0)
113 10
            ? $width . 'x' . $height
114 10
            : static::DEFAULT_DIMENSION;
115
116 10
        return $size;
117
    }
118
119
    /**
120
     * @return CropImageDOInterface
121
     */
122 1
    public function getCrop()
123
    {
124 1
        return $this->crop;
125
    }
126
127
    /**
128
     * @param CropImageDOInterface $crop
0 ignored issues
show
Documentation introduced by
Should the type for parameter $crop not be null|CropImageDOInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
129
     * @return ResourceImageDO
130
     */
131 4
    public function setCrop(CropImageDOInterface $crop = null)
132
    {
133 4
        $this->crop = $crop;
134 4
        return $this;
135
    }
136
137
    /**
138
     * @return array
139
     */
140 2
    public function toArray()
141
    {
142 2
        $data = parent::toArray();
143 2
        if ($this->crop) {
144 1
            $data['crop'] = $this->crop->toArray();
145 1
        } else {
146 1
            unset($data['crop']);
147
        }
148 2
        $data[self::TOKEN_DIMENSION] = '' . $this->getDimension();
149
150 2
        return $data;
151
    }
152
153
}