Completed
Push — master ( 2991a0...bc5f9a )
by Ryan
03:06
created

Feed   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 26

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D getHandler() 0 38 17
C getAlias() 0 24 8
1
<?php
2
/**
3
 * Copyright (c) 2017–2018 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2018 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Type;
12
13
use Psr\Log\NullLogger;
14
use SimplePie\Configuration as C;
15
use SimplePie\Exception\SimplePieException;
16
use SimplePie\Mixin as Tr;
17
use SimplePie\Parser\Date as DateParser;
18
use stdClass;
19
20
/**
21
 * The top-most element in a feed.
22
 *
23
 * @method SimplePie\Type\Person getAuthor(string $namespaceAlias) Returns the Author associated with this feed.
24
 * @method SimplePie\Type\Category[] getCategories(string $namespaceAlias) Returns the list of Categories/Tags/Topics
25
 *         associated with this feed.
26
 * @method SimplePie\Type\Person[] getContributors(string $namespaceAlias) Returns the list of Contributors associated
27
 *         with this feed.
28
 * @method SimplePie\Type\Entry[] getEntries(string $namespaceAlias) Returns the list of Entries/Items associated with
29
 *         this feed.
30
 * @method SimplePie\Type\Generator getGenerator(string $namespaceAlias) Returns the Generator associated with
31
 *         this feed.
32
 * @method string getId(string $namespaceAlias) Returns the ID associated with this feed.
33
 * @method SimplePie\Type\Image getIcon(string $namespaceAlias) Returns the Icon associated with this feed.
34
 * @method SimplePie\Type\Entry[] getItems(string $namespaceAlias) Alias for `getEntries()`.
35
 * @method string getLang(string $namespaceAlias) Alias for `getLanguage()`.
36
 * @method string getLanguage(string $namespaceAlias) Returns the language associated with this feed.
37
 * @method SimplePie\Type\Link[] getLinks(string $namespaceAlias) Returns the list of Links associated with this feed.
38
 * @method SimplePie\Type\Image getLogo(string $namespaceAlias) Returns the Logo associated with this feed.
39
 * @method \DateTime getPubDate(string $namespaceAlias) Alias for `getPublished()`.
40
 * @method \DateTime getPublished(string $namespaceAlias) Returns the date that the feed was published.
41
 * @method string getRights(string $namespaceAlias) Returns the copyright information associated with this feed.
42
 * @method string getSubtitle(string $namespaceAlias) Returns the sub-title associated with this feed.
43
 * @method string getSummary(string $namespaceAlias) Returns the summary associated with this feed.
44
 * @method string getTitle(string $namespaceAlias) Returns the title associated with this feed.
45
 * @method \DateTime getUpdated(string $namespaceAlias) Returns the date that the feed was updated.
46
 */
47
class Feed extends AbstractType implements BranchInterface, C\SetLoggerInterface
48
{
49
    use Tr\DateTrait;
50
    use Tr\DeepTypeTrait;
51
    use Tr\LoggerTrait;
52
    use Tr\RootTrait;
53
54
    /**
55
     * The preferred namespace alias for a given XML namespace URI. Should be
56
     * the result of a call to `SimplePie\Util\Ns`.
57
     *
58
     * @var string
59
     */
60
    protected $namespaceAlias;
61
62
    /**
63
     * Constructs a new instance of this class.
64
     *
65
     * @param string $namespaceAlias [description]
66
     */
67
    public function __construct(string $namespaceAlias)
68
    {
69
        $this->root           = new stdClass();
70
        $this->logger         = new NullLogger();
71
        $this->namespaceAlias = $namespaceAlias;
72
    }
73
74
    /**
75
     * Finds the common internal alias for a given method name.
76
     *
77
     * @param string $nodeName The name of the method being called.
78
     *
79
     * @return string
80
     *
81
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
82
     */
83
    public function getAlias(string $nodeName): string
84
    {
85
        switch ($nodeName) {
86
            case 'categories':
87
                return 'category';
88
89
            case 'contributors':
90
                return 'contributor';
91
92
            case 'entries':
93
            case 'items':
94
                return 'entry';
95
96
            case 'language':
97
                return 'lang';
98
99
            case 'links':
100
                return 'link';
101
102
            case 'pubDate':
103
                return 'published';
104
105
            default:
106
                return $nodeName;
107
        }
108
    }
109
110
    // phpcs:enable
111
112
    /**
113
     * Get the correct handler for a whitelisted method name.
114
     *
115
     * @param string $nodeName The name of the method being called.
116
     * @param array  $args     Any arguments passed into that method. The default value is an empty array.
117
     *
118
     * @throws SimplePieException
119
     *
120
     * @return mixed Either `TypeInterface` or `TypeInterface[]`.
121
     *
122
     * phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
123
     */
124
    public function getHandler(string $nodeName, array $args = [])
125
    {
126
        switch ($nodeName) {
127
            case 'id':
128
            case 'lang':
129
            case 'rights':
130
            case 'subtitle':
131
            case 'summary':
132
            case 'title':
133
                return $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null);
134
135
            case 'published':
136
            case 'updated':
137
                return (new DateParser(
138
                    $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null)->getValue(),
139
                    $this->outputTimezone,
140
                    $this->createFromFormat
141
                ))->getDateTime();
142
143
            case 'author':
144
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Person::class, $args[0] ?? null);
145
146
            case 'generator':
147
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Generator::class, $args[0] ?? null);
148
149
            case 'icon':
150
            case 'logo':
151
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Image::class, $args[0] ?? null);
152
153
            case 'category':
154
            case 'contributor':
155
            case 'entry':
156
            case 'link':
157
                return $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
158
159
            default:
160
                throw new SimplePieException(
161
                    $this->getUnresolvableMessage($nodeName)
162
                );
163
        }
164
    }
165
166
    // phpcs:enable
167
}
168