Passed
Branch master (d27d35)
by Kane
13:10
created

HtmlBuilder::getTypeOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Cohensive\OEmbed;
3
4
class HtmlBuilder
5
{
6
    const TYPE_RAW = 'raw';
7
    const TYPE_IFRAME = 'iframe';
8
    const TYPE_VIDEO = 'video';
9
10
    public function __construct(
11
        protected string $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

11
        /** @scrutinizer ignore-unused */ protected string $type,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
        protected string|array $html,
0 ignored issues
show
Unused Code introduced by
The parameter $html is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

12
        /** @scrutinizer ignore-unused */ protected string|array $html,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
        protected string|null $script = null
0 ignored issues
show
Bug introduced by
The type Cohensive\OEmbed\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Unused Code introduced by
The parameter $script is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

13
        /** @scrutinizer ignore-unused */ protected string|null $script = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    ) {
15
    }
16
17
    /**
18
     * Returns current type.
19
     */
20
    public function type(): string
21
    {
22
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
23
    }
24
25
    /**
26
     * Returns HTML code for media provider.
27
     */
28
    public function html(array $options = [], bool $amp = false): string
29
    {
30
        if (is_array($this->html)) {
0 ignored issues
show
Bug Best Practice introduced by
The property html does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
31
            $attrs = $this->applyOptions($this->html, $options);
32
        }
33
34
        if ($this->type === self::TYPE_IFRAME) {
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
35
            return $this->iframe($attrs, $amp);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attrs does not seem to be defined for all execution paths leading up to this point.
Loading history...
36
        }
37
38
        if ($this->type === self::TYPE_VIDEO) {
39
            return $this->video($attrs, $amp);
40
        }
41
42
        return $this->html;
43
    }
44
45
    /**
46
     * Return AMP-friendly HTML for media provider.
47
     */
48
    public function ampHtml(array $options = []): string
49
    {
50
        return $this->html($options, true);
51
    }
52
53
    /**
54
     * Constructs <iframe> HTML-element based on array of provider attributes.
55
     */
56
    protected function iframe(array $attrs, bool $amp = false): string
57
    {
58
        $tag = $amp ? 'amp-iframe' : 'iframe';
59
60
        $html = "<$tag";
61
        foreach ($attrs as $attr => $val) {
62
            $html .= sprintf(' %s="%s"', $attr, $val);
63
        }
64
        $html .= "></$tag>";
65
66
        return $html;
67
    }
68
69
    /**
70
     * Constructs <video> HTML-element based on an array of provider attributes.
71
     */
72
    protected function video(array $attrs, bool $amp = false): string
73
    {
74
        $tag = $amp ? 'amp-video' : 'video';
75
76
        $inner = '';
77
78
        $html = "<$tag";
79
        foreach ($attrs as $attr => $val) {
80
            if (is_array($val)) {
81
                foreach ($val as $child) {
82
                    $inner .= "<$attr";
83
                    foreach ($child as $iattr => $ival) {
84
                        $inner .= sprintf(' %s="%s"', $iattr, $ival);
85
                    }
86
                    $inner .= ">";
87
                }
88
            } else {
89
                $html .= sprintf(' %s="%s"', $attr, $val);
90
            }
91
        }
92
        $html .= ">";
93
94
        $html .= $inner;
95
96
        $html .= "</$tag>";
97
98
        return $html;
99
    }
100
101
    /**
102
     * Returns script source if available.
103
     */
104
    public function script(): ?string
105
    {
106
        return $this->script;
0 ignored issues
show
Bug Best Practice introduced by
The property script does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
107
    }
108
109
    /**
110
     * Converts class to an array.
111
     */
112
    public function toArray(): array
113
    {
114
        return [
115
            'type' => $this->type,
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
116
            'html' => $this->html,
0 ignored issues
show
Bug Best Practice introduced by
The property html does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
117
        ];
118
    }
119
120
    /**
121
     * Extracts and returns an array of options for a current HTML element type.
122
     */
123
    protected function getTypeOptions(array $options): array
124
    {
125
        if (isset($options['html'])) {
126
            return $options['html'][$this->type] ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Cohensive\OEmbed\HtmlBuilder. Did you maybe forget to declare it?
Loading history...
127
        }
128
129
        return [];
130
    }
131
132
    /**
133
     * Merge and apply local and global options to the provider attributes.
134
     */
135
    protected function applyOptions(array $attrs, array $options): array
136
    {
137
        $width = $options['width'] ?? null;
138
        $height = $options['height'] ?? null;
139
140
        if (isset($attrs['width']) && isset($attrs['height'])) {
141
            $ratio = $attrs['width'] / $attrs['height'];
142
143
            if ($width) {
144
                $attrs['width'] = $width;
145
            } else {
146
                $attrs['width'] = (int) ($attrs['height'] * $ratio);
147
            }
148
149
            if ($height) {
150
                $attrs['height'] = $height;
151
            } else {
152
                $attrs['height'] = (int) ($attrs['width'] / $ratio);
153
            }
154
        }
155
156
        $typeOptions = $this->getTypeOptions($options);
157
        $attrs = array_merge($attrs, $typeOptions);
158
159
        return $attrs;
160
    }
161
}
162