BaseParser::getCollector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BramR\TorExits\Parser;
4
5
use BramR\TorExits\Collector\CollectorInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LoggerAwareTrait;
8
use Psr\Log\NullLogger;
9
use Psr\Http\Message\StreamInterface;
10
11
abstract class BaseParser implements ParserInterface
12
{
13
    use LoggerAwareTrait;
14
15
    protected $collector;
16
    protected $parseWarningThreshold;
17
18
    const DEFAULT_PARSE_WARNING_THRESHOLD = 500;
19
20 7
    public function __construct(CollectorInterface $collector = null)
21
    {
22 7
        $this->setLogger(new NullLogger());
23 7
        if ($collector) {
24 1
            $this->setCollector($collector);
25 1
        }
26 7
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function setCollector(CollectorInterface $collector)
32
    {
33 2
        $this->collector = $collector;
34 2
        return $this;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3
    public function getCollector()
41
    {
42 3
        return $this->collector;
43
    }
44
45
    /**
46
     * Getter for parseWarningThreshold
47
     *
48
     * return string
49
     */
50 4
    public function getParseWarningThreshold()
51
    {
52 4
        return is_null($this->parseWarningThreshold) ?
53 4
            self::DEFAULT_PARSE_WARNING_THRESHOLD : $this->parseWarningThreshold;
54
    }
55
56
    /**
57
     * Setter for parseWarningThreshold
58
     *
59
     * @param int $parseWarningThreshold
60
     * @return BaseParser
61
     */
62 2
    public function setParseWarningThreshold($parseWarningThreshold)
63
    {
64 2
        $this->parseWarningThreshold = (int) $parseWarningThreshold;
65 2
        return $this;
66
    }
67
68
69
    /**
70
     * Return data stream, either from passed in stream or from the collector.
71
     *
72
     * @throws Exception if no StreamInterface can be retrurned
73
     *
74
     * @param StreamInterface $data = null
75
     * @return StreamInterface
76
     */
77 6
    protected function getStreamData(StreamInterface $data = null)
78
    {
79 6
        if (!is_null($data)) {
80 3
            return $data;
81
        }
82 3
        if ($this->getCollector()) {
83
            try {
84 2
                return $this->getCollector()->fetch();
85 1
            } catch (\Exception $e) {
86 1
                $this->logger->error('Failed getting data from collector: ' . $e->getMessage());
87 1
                throw $e;
88
            }
89
        } else {
90 1
            $this->logger->error('Cannot find stream to parse');
91 1
            throw new \InvalidArgumentException('Cannot find stream to parse.');
92
        }
93
    }
94
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    abstract public function parse(StreamInterface $data = null);
100
}
101