Thumbnail   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 106
rs 10
c 5
b 0
f 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 6 1
A makeMultiple() 0 10 2
A name() 0 4 1
A filters() 0 4 1
A width() 0 4 1
A height() 0 4 1
A size() 0 4 1
A getFirst() 0 8 3
1
<?php namespace Modules\Media\Image;
2
3
class Thumbnail
4
{
5
    /**
6
     * @var array
7
     */
8
    private $filters;
9
    /**
10
     * @var string
11
     */
12
    private $name;
13
14
    /**
15
     * @param $name
16
     * @param $filters
17
     */
18
    private function __construct($name, $filters)
19
    {
20
        $this->filters = $filters;
21
        $this->name = $name;
22
    }
23
24
    /**
25
     * @param $thumbnailDefinition
26
     * @return static
27
     */
28
    public static function make($thumbnailDefinition)
29
    {
30
        $name = key($thumbnailDefinition);
31
32
        return new static($name, $thumbnailDefinition[$name]);
33
    }
34
35
    /**
36
     * Make multiple thumbnail classes with the given array
37
     * @param array $thumbnailDefinitions
38
     * @return array
39
     */
40
    public static function makeMultiple(array $thumbnailDefinitions)
41
    {
42
        $thumbnails = [];
43
44
        foreach ($thumbnailDefinitions as $name => $thumbnail) {
45
            $thumbnails[] = self::make([$name => $thumbnail]);
46
        }
47
48
        return $thumbnails;
49
    }
50
51
    /**
52
     * Return the thumbnail name
53
     * @return string
54
     */
55
    public function name()
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function filters()
64
    {
65
        return $this->filters;
66
    }
67
68
    /**
69
     * Return the first width option found in the filters
70
     * @return int
71
     */
72
    public function width()
73
    {
74
        return $this->getFirst('width');
75
    }
76
77
    /**
78
     * Return the first height option found in the filters
79
     * @return int
80
     */
81
    public function height()
82
    {
83
        return $this->getFirst('height');
84
    }
85
86
    /**
87
     * Get the thumbnail size in format: width x height
88
     * @return string
89
     */
90
    public function size()
91
    {
92
        return $this->width() . 'x' . $this->height();
93
    }
94
95
    /**
96
     * Get the first found key in filters
97
     * @param string $key
98
     * @return int
99
     */
100
    private function getFirst($key)
101
    {
102
        foreach ($this->filters as $filter) {
103
            if (isset($filter[$key])) {
104
                return (int) $filter[$key];
105
            }
106
        }
107
    }
108
}
109