Completed
Push — issue/134 ( d0dc5e...07c20b )
by Alex
01:47
created

ParserAbstract::addValidItem()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
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;
12
13
use FeedIo\Parser\UnsupportedFormatException;
14
use FeedIo\Reader\Document;
15
use FeedIo\Feed\ItemInterface;
16
use FeedIo\Feed\NodeInterface;
17
use Psr\Log\LoggerInterface;
18
19
/**
20
 * Parses a document if its format matches the parser's standard
21
 *
22
 * Depends on :
23
 *  - FeedIo\StandardAbstract
24
 *  - Psr\Log\LoggerInterface
25
 *
26
 */
27
abstract class ParserAbstract
28
{
29
30
    /**
31
     * @var \Psr\Log\LoggerInterface
32
     */
33
    protected $logger;
34
35
    /**
36
     * @var array[FilterInterface]
37
     */
38
    protected $filters = array();
39
40
    /**
41
     * @var StandardAbstract
42
     */
43
    protected $standard;
44
45
    /**
46
     * @param StandardAbstract $standard
47
     * @param LoggerInterface  $logger
48
     */
49 40
    public function __construct(StandardAbstract $standard, LoggerInterface $logger)
50
    {
51 40
        $this->standard = $standard;
52 40
        $this->logger = $logger;
53 40
    }
54
55
    /**
56
     * Tries to parse the document
57
     *
58
     * @param Document $document
59
     * @param FeedInterface $feed
60
     * @return \FeedIo\FeedInterface
61
     * @throws \FeedIo\Parser\UnsupportedFormatException
62
     */
63 12
    public function parse(Document $document, FeedInterface $feed)
64
    {
65 12
        if (!$this->standard->canHandle($document)) {
66 1
            throw new UnsupportedFormatException('this is not a supported format');
67
        }
68
69 11
        $this->checkBodyStructure($document, $this->standard->getMandatoryFields());
70 10
        $this->parseContent($document, $feed);
71
72 10
        return $feed;
73
    }
74
75
    /**
76
     * This method is called by parse() if and only if the checkBodyStructure was successful
77
     *
78
     * @param Document $document
79
     * @param FeedInterface $feed
80
     * @return \FeedIo\FeedInterface
81
     */
82
    abstract public function parseContent(Document $document, FeedInterface $feed);
83
84
    /**
85
     * @param  Document            $document
86
     * @param  array               $mandatoryFields
87
     * @return $this
88
     * @throws MissingFieldsException
89
     */
90
    abstract public function checkBodyStructure(Document $document, array $mandatoryFields);
91
92
    /**
93
     * @return StandardAbstract
94
     */
95 25
    public function getStandard()
96
    {
97 25
        return $this->standard;
98
    }
99
100
    /**
101
     * @param  FeedInterface $feed
102
     * @param  NodeInterface $item
103
     * @return $this
104
     */
105 7
    public function addValidItem(FeedInterface $feed, NodeInterface $item)
106
    {
107 7
        if ($item instanceof ItemInterface && $this->isValid($item)) {
108 6
            $feed->add($item);
109 6
        }
110
111 7
        return $this;
112
    }
113
114
    /**
115
     * @param  ItemInterface $item
116
     * @return bool
117
     */
118 9
    public function isValid(ItemInterface $item)
119
    {
120 9
        foreach ($this->filters as $filter) {
121 3
            if (!$filter->isValid($item)) {
122 2
                return false;
123
            }
124 7
        }
125
126 7
        return true;
127
    }
128
129
    /**
130
     * @param  FilterInterface $filter
131
     * @return $this
132
     */
133 5
    public function addFilter(FilterInterface $filter)
134
    {
135 5
        $this->filters[] = $filter;
136
137 5
        return $this;
138
    }
139
140
    /**
141
     * Reset filters
142
     * @return $this
143
     */
144 2
    public function resetFilters()
145
    {
146 2
        $this->filters = [];
147
148 2
        return $this;
149
    }
150
}
151