GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cb0259...f658ab )
by Christian
08:24
created

AbstractCrawler::parseDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\LastFm\Crawler;
11
12
use Core23\LastFm\Connection\ConnectionInterface;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
abstract class AbstractCrawler
16
{
17
    public const URL_PREFIX  = 'http://last.fm';
18
19
    public const NEWLINE = "\n";
20
21
    /**
22
     * @var ConnectionInterface
23
     */
24
    protected $connection;
25
26
    /**
27
     * AbstractService constructor.
28
     *
29
     * @param ConnectionInterface $connection
30
     */
31
    public function __construct(ConnectionInterface $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * Crawles a url.
38
     *
39
     * @param string $url
40
     *
41
     * @return Crawler|null
42
     */
43
    final protected function crawl(string $url): ? Crawler
44
    {
45
        if ($content = $this->connection->getPageBody($url)) {
46
            return new Crawler($content);
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * Parses a url node.
54
     *
55
     * @param Crawler $node
56
     * @param string  $attr
57
     *
58
     * @return null|string
59
     */
60
    final protected function parseUrl(Crawler $node, string $attr = 'href') : ? string
61
    {
62
        if (0 === $node->count()) {
63
            return null;
64
        }
65
66
        if ($url = $node->attr($attr)) {
67
            return preg_replace('/^\//', static::URL_PREFIX.'/', $url);
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * Parses an image node.
75
     *
76
     * @param Crawler $node
77
     *
78
     * @return null|string
79
     */
80
    final protected function parseImage(Crawler $node) : ? string
81
    {
82
        return $this->parseUrl($node, 'src');
83
    }
84
85
    /**
86
     * Parses a string node.
87
     *
88
     * @param Crawler $node
89
     * @param bool    $multiline
90
     *
91
     * @return null|string
92
     */
93
    final protected function parseString(Crawler $node, bool $multiline = false) : ? string
94
    {
95
        if (0 === $node->count()) {
96
            return null;
97
        }
98
99
        $content = $node->attr('content');
100
101
        if (null === $content) {
102
            if ($multiline) {
103
                $content = $node->html();
104
                $content = preg_replace('/<p[^>]*?>/', '', $content);
105
                $content = str_replace('</p>', static::NEWLINE, $content);
106
                $content = preg_replace('/<br\s?\/?>/i', static::NEWLINE, $content);
107
            } else {
108
                $content = $node->text();
109
            }
110
        }
111
112
        return trim(strip_tags($content));
113
    }
114
115
    /**
116
     * Parses a date note.
117
     *
118
     * @param Crawler $node
119
     *
120
     * @return \DateTime|null
121
     */
122
    final protected function parseDate(Crawler $node) : ? \DateTime
123
    {
124
        $content = $this->parseString($node);
125
126
        if (null !== $content) {
127
            return new \DateTime($content);
128
        }
129
130
        return null;
131
    }
132
}
133