Completed
Push — master ( eb49de...2435ec )
by Paul
8s
created

Icon   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.24%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 108
ccs 37
cts 41
cp 0.9024
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 4 1
A offsetGet() 0 4 2
B findBestForSize() 0 12 5
B findBest() 0 16 5
A offsetSet() 0 11 3
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
3
namespace Elphin\IcoFileLoader;
4
5
/**
6
 * An instance of icon holds the extracted data from a .ico file
7
 */
8
class Icon implements \ArrayAccess, \Countable
9
{
10
    /**
11
     * @var IconImage[]
12
     */
13
    private $images = [];
14
15
    /**
16
     * Returns best icon image with dimensions matching w,h
17
     * @param $w
18
     * @param $h
19
     * @return IconImage|null
20
     */
21 1
    public function findBestForSize($w, $h)
22
    {
23 1
        $bestBitCount = 0;
24 1
        $best = null;
25 1
        foreach ($this->images as $image) {
26 1
            if ($image->width == $w && $image->height == $h && ($image->bitCount > $bestBitCount)) {
27
                $bestBitCount = $image->bitCount;
28
                $best = $image;
29
            }
30 1
        }
31 1
        return $best;
32
    }
33
34
    /**
35
     * Finds the highest quality image in the icon
36
     * @return IconImage
37
     */
38 1
    public function findBest()
39
    {
40 1
        $bestBitCount = 0;
41 1
        $bestWidth = 0;
42 1
        $best = null;
43 1
        foreach ($this->images as $image) {
44 1
            if (($image->width > $bestWidth) ||
45
                (($image->width == $bestWidth) && ($image->bitCount > $bestBitCount))
46 1
            ) {
47 1
                $bestWidth = $image->width;
48 1
                $bestBitCount = $image->bitCount;
49 1
                $best = $image;
50 1
            }
51 1
        }
52 1
        return $best;
53
    }
54
55
    /**
56
     * Count number of images in the icon
57
     * As this class implements Countable you can simply use count($icon) if you desire
58
     * @return int
59
     */
60 18
    public function count()
61
    {
62 18
        return count($this->images);
63
    }
64
65
    /**
66
     * Set an icon
67
     * This is an implementation of ArrayAccess allowing you to do $icon[$x]=$image
68
     * @param integer $offset
69
     * @param IconImage $value
70
     */
71 19
    public function offsetSet($offset, $value)
72
    {
73 19
        if (!$value instanceof IconImage) {
74 1
            throw new \InvalidArgumentException('Can only add IconImage instances to an Icon');
75
        }
76 18
        if (is_null($offset)) {
77 18
            $this->images[] = $value;
78 18
        } else {
79 1
            $this->images[$offset] = $value;
80
        }
81 18
    }
82
83
    /**
84
     * Check if image with particular index exists
85
     * This is an implementation of ArrayAccess allowing you to do isset($icon[$x])
86
     * @param integer $offset
87
     * @return boolean
88
     */
89 1
    public function offsetExists($offset)
90
    {
91 1
        return isset($this->images[$offset]);
92
    }
93
94
    /**
95
     * Remove image from icon
96
     * This is an implementation of ArrayAccess allowing you to do unset($icon[$x])
97
     * @param integer $offset
98
     * @return boolean
99
     */
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->images[$offset]);
103 1
    }
104
105
    /**
106
     * Get image from icon
107
     * This is an implementation of ArrayAccess allowing you to do $image = $icon[$x]
108
     * @param integer $offset
109
     * @return IconImage
110
     */
111 17
    public function offsetGet($offset)
112
    {
113 17
        return isset($this->images[$offset]) ? $this->images[$offset] : null;
114
    }
115
}
116