Completed
Push — issue/96 ( 5d401e...7b0d36 )
by Alex
02:40
created

JsonParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 63
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseContent() 0 14 2
A checkBodyStructure() 0 4 1
A parseItems() 0 14 2
A readOffset() 0 8 2
1
<?php
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
14
use FeedIo\Feed\Item;
15
use FeedIo\FeedInterface;
16
use FeedIo\MissingFieldsException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FeedIo\Parser\MissingFieldsException.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use FeedIo\ParserAbstract;
18
use FeedIo\Reader\Document;
19
use FeedIo\Rule\Json\RuleSet;
20
use FeedIo\Rule\Json\Title;
21
22
class JsonParser extends ParserAbstract
23
{
24
25
    /**
26
     * @param Document $document
27
     * @param FeedInterface $feed
28
     * @return FeedInterface
29
     */
30 1
    public function parseContent(Document $document, FeedInterface $feed)
31
    {
32 1
        $data = $document->getJsonAsArray();
33 1
        $feed->setTitle($this->readOffset($data, 'title'));
34 1
        $feed->setDescription($this->readOffset($data, 'description'));
35 1
        $feed->setLink($this->readOffset($data, 'feed_url'));
36 1
        $feed->setUrl($this->readOffset($data, 'home_page_url'));
37
38 1
        if ( array_key_exists('items', $data) ) {
39 1
            $this->parseItems($data['items'], $feed);
40 1
        }
41
42 1
        return $feed;
43
    }
44
45 1
    public function checkBodyStructure(Document $document, array $mandatoryFields)
46
    {
47
        // TODO: Implement checkBodyStructure() method.
48 1
    }
49
50
    /**
51
     * @param array $items
52
     * @param FeedInterface $feed
53
     * @return $this
54
     */
55 1
    public function parseItems(array $items, FeedInterface $feed)
56
    {
57 1
        foreach( $items as $dataItem ) {
58 1
            $item = new Item();
59 1
            $item->setPublicId($this->readOffset($dataItem, 'id'));
60 1
            $item->setTitle($this->readOffset($dataItem, 'title'));
61 1
            $item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published')));
62 1
            $contentHtml = $this->readOffset($dataItem, 'content_html');
63 1
            $item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
64 1
            $feed->add($item);
65 1
        }
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * @param array $data
72
     * @param string $offsetName
73
     * @param mixed $default
74
     * @return mixed
75
     */
76 1
    public function readOffset(array $data, $offsetName, $default = null)
77
    {
78 1
        if (array_key_exists($offsetName, $data) ){
79 1
            return $data[$offsetName];
80
        }
81
82 1
        return $default;
83
    }
84
}