Entry   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 28
eloc 62
c 2
b 0
f 0
dl 0
loc 151
ccs 46
cts 60
cp 0.7667
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
C getHandler() 0 43 15
B getAlias() 0 29 9
A getNode() 0 3 1
A __construct() 0 12 2
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
use SimplePie\Parser\Date as DateParser;
20
21
/**
22
 * A type model for an Entry element.
23
 *
24
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#412-the-atomentry-element
25
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#535-items
26
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#55-item
27
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#elements-of-item
28
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#items
29
 *
30
 * @phpcs:disable Generic.Files.LineLength.MaxExceeded
31
 *
32
 * @method array getAuthors(string $namespaceAlias) Returns the Authors associated with this entry.
33
 * @method array getCategories(string $namespaceAlias) Returns the list of Categories/Tags/Topics associated with this entry.
34
 * @method SimplePie\Type\Node getContent(string $namespaceAlias) Returns the content of the entry, serialized as TEXT, HTML, or XHTML content.
35
 * @method array getContributors(string $namespaceAlias) Returns the list of Contributors associated with this entry.
36
 * @method SimplePie\Type\Node getCopyright(string $namespaceAlias) Alias for `getRights()`.
37
 * @method SimplePie\Type\Node getGuid(string $namespaceAlias) Alias for `getId()`.
38
 * @method SimplePie\Type\Node getId(string $namespaceAlias) Returns the ID associated with this entry.
39
 * @method SimplePie\Type\Node getLang(string $namespaceAlias) Alias for `getLanguage()`.
40
 * @method SimplePie\Type\Node getLanguage(string $namespaceAlias) Returns the language associated with this entry.
41
 * @method array getLinks(string $namespaceAlias, string $relFilter) Returns the list of Links associated with this entry.
42
 * @method \DateTime getPubDate(string $namespaceAlias) Alias for `getPublished()`.
43
 * @method \DateTime getPublished(string $namespaceAlias) Returns the date that the entry was published.
44
 * @method SimplePie\Type\Node getRights(string $namespaceAlias) Returns the copyright information associated with this entry.
45
 * @method SimplePie\Type\Node getSummary(string $namespaceAlias) Returns the summary associated with this entry.
46
 * @method SimplePie\Type\Node getTitle(string $namespaceAlias) Returns the title associated with this entry.
47
 * @method \DateTime getUpdated(string $namespaceAlias) Returns the date that the entry was updated.
48
 *
49
 * @phpcs:enable
50
 */
51
class Entry extends AbstractType implements BranchInterface, C\SetLoggerInterface, NodeInterface
52
{
53
    use Tr\DateTrait;
54
    use Tr\DeepTypeTrait;
55
    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...
56
57
    /**
58
     * The DOMNode element to parse.
59
     *
60
     * @var DOMNode
61
     */
62
    protected $node;
63
64
    /**
65
     * The preferred namespace alias for a given XML namespace URI. Should be
66
     * the result of a call to `SimplePie\Util\Ns`.
67
     *
68
     * @var string
69
     */
70
    protected $namespaceAlias;
71
72
    /**
73
     * Constructs a new instance of this class.
74
     *
75
     * @param string          $namespaceAlias [description]
76
     * @param DOMNode|null    $node           The `DOMNode` element to parse.
77
     * @param LoggerInterface $logger         The PSR-3 logger.
78
     *
79
     * @phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
80
     */
81 220
    public function __construct(
82
        string $namespaceAlias,
83
        ?DOMNode $node = null,
84
        LoggerInterface $logger = null
85
    ) {
86
        // @phpcs:enable
87
88 220
        $this->namespaceAlias = $namespaceAlias;
89
90 220
        if ($node) {
91 220
            $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...
92 220
            $this->node   = $node;
93
        }
94 220
    }
95
96
    /**
97
     * Converts this object into a string representation.
98
     */
99
    public function __toString(): string
100
    {
101
        return \sprintf('<%s: resource %s>', \get_called_class(), \md5(\spl_object_hash($this)));
102
    }
103
104
    /**
105
     * Gets the DOMNode element.
106
     */
107
    public function getNode(): ?DOMNode
108
    {
109
        return $this->node;
110
    }
111
112
    /**
113
     * Finds the common internal alias for a given method name.
114
     *
115
     * @param string $nodeName The name of the method being called.
116
     */
117 180
    public function getAlias(string $nodeName): string
118
    {
119 180
        switch ($nodeName) {
120 180
            case 'authors':
121 10
                return 'author';
122
123 170
            case 'categories':
124 4
                return 'category';
125
126 166
            case 'contributors':
127 8
                return 'contributor';
128
129 158
            case 'copyright':
130
                return 'rights';
131
132 158
            case 'guid':
133 1
                return 'id';
134
135 157
            case 'language':
136
                return 'lang';
137
138 157
            case 'links':
139 20
                return 'link';
140
141 137
            case 'pubDate':
142
                return 'published';
143
144
            default:
145 137
                return $nodeName;
146
        }
147
    }
148
149
    /**
150
     * Get the correct handler for a whitelisted method name.
151
     *
152
     * @param string $nodeName The name of the method being called.
153
     * @param array  $args     Any arguments passed into that method.
154
     *
155
     * @throws SimplePieException
156
     *
157
     * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
158
     */
159 180
    public function getHandler(string $nodeName, array $args = [])
160
    {
161 180
        switch ($nodeName) {
162 180
            case 'base':
163 180
            case 'content':
164 161
            case 'id':
165 151
            case 'lang':
166 146
            case 'rights':
167 140
            case 'summary':
168 127
            case 'title':
169 138
                return $this->getScalarSingleValue($this, $nodeName, $args[0]);
170
171 42
            case 'published':
172 42
            case 'updated':
173
                return (new DateParser(
174
                    $this->getScalarSingleValue($this, $nodeName, $args[0])->getValue(),
175
                    $this->outputTimezone,
176
                    $this->createFromFormat
177
                ))->getDateTime();
178
179 42
            case 'author':
180 32
            case 'category':
181 28
            case 'contributor':
182 22
                return $this->getComplexMultipleValues($this, $nodeName, $args[0]);
183
184 20
            case 'link':
185 20
                $links = $this->getComplexMultipleValues($this, $nodeName, $args[0]);
186
187 20
                if (isset($args[1])) {
188 2
                    $relFilter = $args[1];
189
190 2
                    return \array_values(
191
                        \array_filter($links, static function (Link $link) use ($relFilter) {
192 2
                            return $relFilter === $link->getRel()->getValue();
193 2
                        })
194
                    );
195
                }
196
197 18
                return $links;
198
199
            default:
200
                throw new SimplePieException(
201
                    $this->getUnresolvableMessage($nodeName)
202
                );
203
        }
204
    }
205
206
    // @phpcs:enable
207
}
208