Completed
Pull Request — master (#239)
by
unknown
01:51
created

JsonParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 6
dl 0
loc 87
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseContent() 0 14 2
A checkBodyStructure() 0 12 3
A parseItems() 0 24 3
A readOffset() 0 8 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Parser;
12
13
use FeedIo\Feed\Item;
14
use FeedIo\Feed\Item\Author;
15
use FeedIo\FeedInterface;
16
use FeedIo\ParserAbstract;
17
use FeedIo\Reader\Document;
18
19
class JsonParser extends ParserAbstract
20
{
21
22
    /**
23
     * @param Document $document
24
     * @param FeedInterface $feed
25
     * @return FeedInterface
26
     */
27 1
    public function parseContent(Document $document, FeedInterface $feed) : FeedInterface
28
    {
29 1
        $data = $document->getJsonAsArray();
30 1
        $feed->setTitle($this->readOffset($data, 'title'));
31 1
        $feed->setDescription($this->readOffset($data, 'description'));
32 1
        $feed->setLink($this->readOffset($data, 'feed_url'));
33 1
        $feed->setUrl($this->readOffset($data, 'home_page_url'));
34
35 1
        if (array_key_exists('items', $data)) {
36 1
            $this->parseItems($data['items'], $feed);
37
        }
38
39 1
        return $feed;
40
    }
41
42
    /**
43
     * @param Document $document
44
     * @param iterable $mandatoryFields
45
     * @throws MissingFieldsException
46
     * @return bool
47
     */
48 1
    public function checkBodyStructure(Document $document, iterable $mandatoryFields) : bool
49
    {
50 1
        $data = $document->getJsonAsArray();
51
52 1
        foreach ($mandatoryFields as $mandatoryField) {
53 1
            if (! array_key_exists($mandatoryField, $data)) {
54
                throw new MissingFieldsException("Missing {$mandatoryField} in the JSON Feed");
55
            }
56
        }
57
58 1
        return true;
59
    }
60
61
    /**
62
     * @param iterable $items
63
     * @param FeedInterface $feed
64
     * @return JsonParser
65
     */
66 1
    public function parseItems(iterable $items, FeedInterface $feed) : JsonParser
67
    {
68 1
        foreach ($items as $dataItem) {
69 1
            $item = new Item();
70 1
            $item->setPublicId($this->readOffset($dataItem, 'id'));
71 1
            $item->setTitle($this->readOffset($dataItem, 'title'));
72 1
            $item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published')));
73 1
            $contentHtml = $this->readOffset($dataItem, 'content_html');
74 1
            $item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
75 1
            $item->setLink($this->readOffset($dataItem, 'url'));
76
77 1
            if (array_key_exists('author', $dataItem)) {
78 1
                $authorItem = $dataItem['author'];
79 1
                $author = new Author();
80 1
                $author->setName($this->readOffset($authorItem, 'name'));
81 1
                $author->setUri($this->readOffset($authorItem, 'url'));
82 1
                $author->setEmail($this->readOffset($authorItem, 'email'));
83 1
                $item->setAuthor($author);
84
            }
85 1
            $feed->add($item);
86
        }
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * @param array $data
93
     * @param string $offsetName
94
     * @param string|null $default
95
     * @return null|string
96
     */
97 1
    public function readOffset(array $data, string $offsetName, string $default = null) : ? string
98
    {
99 1
        if (array_key_exists($offsetName, $data)) {
100 1
            return $data[$offsetName];
101
        }
102
103 1
        return $default;
104
    }
105
}
106