CrawlerAbstract   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 84
ccs 16
cts 16
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
setUrlParser() 0 1 ?
A __construct() 0 6 1
A getCrawler() 0 4 1
A getUrlParser() 0 4 1
A getFieldValue() 0 19 3
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use function foo\func;
6
use Lbc\Filter\DefaultSanitizer;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
/**
10
 * Class CrawlerAbstract
11
 * @package Lbc\Crawler
12
 */
13
abstract class CrawlerAbstract
14
{
15
    /**
16
     * @var
17
     */
18
    protected $url;
19
20
    /**
21
     * @var string
22
     */
23
    protected $sheme = 'https';
24
25
    /**
26
     * @var Crawler
27
     */
28
    protected $node;
29
30
    /**
31
     * CrawlerAbstract constructor.
32
     * @param Crawler $node
33
     * @param $url
34
     */
35 34
    public function __construct(Crawler $node, $url)
36
    {
37 34
        $this->node = $node;
38
39 34
        $this->setUrlParser($url);
40 34
    }
41
42
    /**
43
     * Return the current node
44
     *
45
     * @return Crawler
46
     */
47 2
    public function getCrawler()
48
    {
49 2
        return $this->node;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55 12
    public function getUrlParser()
56
    {
57 12
        return $this->url;
58
    }
59
60
    /**
61
     * Return the field's value
62
     *
63
     * @param Crawler $node
64
     * @param mixed $defaultValue
65
     * @param \Closure $callback
66
     * @param string $funcName
67
     * @param string $funcParam
68
     *
69
     * @return mixed
70
     */
71 20
    protected function getFieldValue(
72
        Crawler $node,
73
        $defaultValue,
74
        $callback = null,
75
        $funcName = 'text',
76
        $funcParam = ''
77
    ) {
78 20
        if ($callback == null) {
79 20
            $callback = function ($value) {
80 18
                return (new DefaultSanitizer)->clean($value);
81 20
            };
82
        }
83
84 20
        if ($node->count()) {
85 18
            return $callback($node->$funcName($funcParam));
86
        }
87
88 12
        return $defaultValue;
89
    }
90
91
    /**
92
     * @param $url
93
     * @return mixed
94
     */
95
    abstract protected function setUrlParser($url);
96
}
97