Link   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 43
c 2
b 0
f 0
dl 0
loc 135
ccs 37
cts 37
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getAlias() 0 19 7
A __toString() 0 3 1
A getNode() 0 3 1
B getHandler() 0 17 7
A __construct() 0 12 3
1
<?php
2
/**
3
 * Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2019 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Type;
12
13
use DOMNode;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
use SimplePie\Configuration as C;
17
use SimplePie\Exception\SimplePieException;
18
use SimplePie\Mixin as Tr;
19
20
/**
21
 * A type model for a Link element.
22
 *
23
 * @method SimplePie\Type\Node getHref() Alias for `getUrl()`.
24
 * @method SimplePie\Type\Node getHrefLang() Alias for `getLanguage()`.
25
 * @method SimplePie\Type\Node getLang() Alias for `getLanguage()`.
26
 * @method SimplePie\Type\Node getLanguage() Returns the language of the Link.
27
 * @method SimplePie\Type\Node getLength() Returns the length of the Link, in bytes.
28
 * @method SimplePie\Type\Node getMediaType() Returns the media type (née mime type) of the Link.
29
 * @method SimplePie\Type\Node getRel() Alias for `getRelationship()`.
30
 * @method SimplePie\Type\Node getRelationship() Returns the relationship of the Link.
31
 * @method SimplePie\Type\Node getTitle() Returns the title of the Link.
32
 * @method SimplePie\Type\Node getType() Alias for `getMediaType()`.
33
 * @method SimplePie\Type\Node getUri() Alias for `getUrl()`.
34
 * @method SimplePie\Type\Node getUrl() Returns the URL of the Link.
35
 *
36
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#427-the-atomlink-element
37
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#532-link
38
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#required-channel-elements
39
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#top-level
40
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-iTunes-Podcast-RSS#link
41
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-iTunes-Podcast-RSS#itunesnew-feed-url
42
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Media-RSS#mediabacklinks
43
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Media-RSS#mediapeerlink
44
 */
45
class Link extends AbstractType implements C\SetLoggerInterface, NodeInterface, TypeInterface
46
{
47
    use Tr\LoggerTrait;
0 ignored issues
show
Bug introduced by
The type SimplePie\Mixin\LoggerTrait 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...
48
49
    /**
50
     * The DOMNode element to parse.
51
     *
52
     * @var DOMNode
53
     */
54
    protected $node;
55
56
    /**
57
     * The link's remote location.
58
     *
59
     * @var string
60
     */
61
    protected $href;
62
63
    /**
64
     * The link's relationship to the current document.
65
     *
66
     * @var string
67
     */
68
    protected $rel;
69
70
    /**
71
     * The link's media type.
72
     *
73
     * @var string
74
     */
75
    protected $type;
76
77
    /**
78
     * The language of the link's remote location.
79
     *
80
     * @var string
81
     */
82
    protected $hreflang;
83
84
    /**
85
     * The link's title.
86
     *
87
     * @var string
88
     */
89
    protected $title;
90
91
    /**
92
     * The link's length, in bytes (e.g., if it is a large file or direct download).
93
     *
94
     * @var int
95
     */
96
    protected $length;
97
98
    /**
99
     * Constructs a new instance of this class.
100
     *
101
     * @param DOMNode|null    $node   The `DOMNode` element to parse.
102
     * @param LoggerInterface $logger The PSR-3 logger.
103
     */
104 75
    public function __construct(?DOMNode $node = null, LoggerInterface $logger = null)
105
    {
106 75
        if ($node) {
107 75
            $this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
108 75
            $this->node   = $node;
109
110 75
            foreach ($this->node->attributes as $attribute) {
111 75
                $this->{$attribute->name} = new Node($attribute);
112
            }
113
114 75
            $this->rel  = $this->rel  ?? Node::factory('alternate');
115 75
            $this->type = $this->type ?? Node::factory('application/atom+xml');
116
        }
117 75
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function __toString(): string
123
    {
124 1
        return (string) $this->href ?? '';
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 2
    public function getNode(): ?DOMNode
131
    {
132 2
        return $this->node;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 40
    public function getAlias(string $nodeName): string
139
    {
140 40
        switch ($nodeName) {
141 40
            case 'uri':
142 40
            case 'url':
143 13
                return 'href';
144
145 40
            case 'relationship':
146 18
                return 'rel';
147
148 40
            case 'mediaType':
149 8
                return 'type';
150
151 38
            case 'lang':
152 38
            case 'language':
153 3
                return 'hreflang';
154
155
            default:
156 38
                return $nodeName;
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 40
    public function getHandler(string $nodeName, array $args = []): Node
164
    {
165
        // Shut up, linter.
166
        $args;
167
168 40
        switch ($nodeName) {
169 40
            case 'href':
170 32
            case 'hreflang':
171 30
            case 'length':
172 27
            case 'rel':
173 11
            case 'title':
174 9
            case 'type':
175 39
                return $this->{$nodeName} ?? new Node();
176
177
            default:
178 1
                throw new SimplePieException(
179 1
                    $this->getUnresolvableMessage($nodeName)
180
                );
181
        }
182
    }
183
}
184