Image   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 48.57%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 112
ccs 17
cts 35
cp 0.4857
rs 10
c 0
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getProperties() 0 30 7
A __construct() 0 3 1
A setAttribute() 0 18 6
1
<?php
2
3
namespace Mpyw\OpenGraph\Elements;
4
5
use Mpyw\OpenGraph\Property;
6
7
/**
8
 * An Open Graph image element.
9
 */
10
class Image extends ElementBase
11
{
12
    /**
13
     * The URL of an image resource associated with the object.
14
     *
15
     * @var string
16
     */
17
    public $url;
18
19
    /**
20
     * An alternate URL to use if an image resource requires HTTPS.
21
     *
22
     * @var null|string
23
     */
24
    public $secureUrl;
25
26
    /**
27
     * The MIME type of an image resource.
28
     *
29
     * @var null|string
30
     */
31
    public $type;
32
33
    /**
34
     * The width of an image resource in pixels.
35
     *
36
     * @var null|int
37
     */
38
    public $width;
39
40
    /**
41
     * The height of an image resource in pixels.
42
     *
43
     * @var null|int
44
     */
45
    public $height;
46
47
    /**
48
     * Whether the image is user-generated or not.
49
     *
50
     * @var null|bool
51
     */
52
    public $userGenerated;
53
54
    /**
55
     * @param mixed $url URL to the image file.
56
     */
57 2
    public function __construct($url)
58
    {
59 2
        $this->url = (string)$url;
60 2
    }
61
62
    /**
63
     * @param string $name
64
     * @param mixed  $value
65
     */
66 2
    public function setAttribute(string $name, $value): void
67
    {
68
        switch ($name) {
69 2
            case Property::IMAGE_HEIGHT:
70 2
                $this->height = (int)$value;
71 2
                break;
72 2
            case Property::IMAGE_WIDTH:
73 2
                $this->width = (int)$value;
74 2
                break;
75 1
            case Property::IMAGE_TYPE:
76 1
                $this->type = (string)$value;
77 1
                break;
78 1
            case Property::IMAGE_SECURE_URL:
79 1
                $this->secureUrl = (string)$value;
80 1
                break;
81
            case Property::IMAGE_USER_GENERATED:
82
                $this->userGenerated = $this->convertToBoolean($value);
83
                break;
84
        }
85 2
    }
86
87
    /**
88
     * Gets all properties set on this element.
89
     *
90
     * @return Property[]
91
     */
92
    public function getProperties(): array
93
    {
94
        $properties = [];
95
96
        // URL must precede all other properties
97
        if ($this->url !== null) {
98
            $properties[] = new Property(Property::IMAGE_URL, $this->url);
99
        }
100
101
        if ($this->height !== null) {
102
            $properties[] = new Property(Property::IMAGE_HEIGHT, $this->height);
103
        }
104
105
        if ($this->secureUrl !== null) {
106
            $properties[] = new Property(Property::IMAGE_SECURE_URL, $this->secureUrl);
107
        }
108
109
        if ($this->type !== null) {
110
            $properties[] = new Property(Property::IMAGE_TYPE, $this->type);
111
        }
112
113
        if ($this->width !== null) {
114
            $properties[] = new Property(Property::IMAGE_WIDTH, $this->width);
115
        }
116
117
        if ($this->userGenerated !== null) {
118
            $properties[] = new Property(Property::IMAGE_USER_GENERATED, $this->userGenerated);
119
        }
120
121
        return $properties;
122
    }
123
}
124