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 ( 547017...c67bb4 )
by Christian
04:20
created

EventInfoCrawler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getEventInfo() 0 24 2
A readArtists() 0 15 2
A readVenues() 0 19 1
A crawlEvent() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\LastFm\Crawler;
13
14
use Core23\LastFm\Model\Artist;
15
use Core23\LastFm\Model\EventInfo;
16
use Core23\LastFm\Model\Venue;
17
use Core23\LastFm\Model\VenueAddress;
18
use Symfony\Component\DomCrawler\Crawler;
19
20
final class EventInfoCrawler extends AbstractCrawler
21
{
22
    /**
23
     * Get all event information.
24
     *
25
     * @param int $id
26
     *
27
     * @return EventInfo|null
28
     */
29
    public function getEventInfo(int $id): ?EventInfo
30
    {
31
        $node = $this->crawlEvent($id);
32
33
        if (null === $node) {
34
            return null;
35
        }
36
37
        $timeNode = $node->filter('.qa-event-date');
38
39
        return new EventInfo(
40
            $id,
41
            $this->parseString($node->filter('h1.header-title')),
42
            $this->parseString($node->filter('.qa-event-description'), true),
43
            $this->parseDate($timeNode->filter('[itemprop="startDate"]')),
0 ignored issues
show
Bug introduced by
It seems like $this->parseDate($timeNo...temprop="startDate"]')) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
44
            $this->parseDate($timeNode->filter('[itemprop="endDate"]')),
45
            $this->parseString($node->filter('.qa-event-link a')),
46
            $this->parseImage($node->filter('.event-poster-preview')),
47
            $this->parseUrl($node->filter('link[rel="canonical"]')),
48
            $node->filter('.namespace--events_festival_overview')->count() > 0,
49
            $this->readVenues($node),
50
            $this->readArtists($node)
51
        );
52
    }
53
54
    /**
55
     * @param Crawler $node
56
     *
57
     * @return Artist[]
58
     */
59
    private function readArtists(Crawler $node): array
60
    {
61
        $artistNode = $node->filter('.grid-items');
62
63
        return $artistNode->filter('.grid-items-item')->each(function (Crawler $node): Artist {
64
            $image = $this->parseImage($node->filter('.grid-items-cover-image-image img'));
65
66
            return new Artist(
67
                $this->parseString($node->filter('.grid-items-item-main-text')),
68
                null,
69
                $image ? [$image] : [],
70
                $this->parseUrl($node->filter('.grid-items-item-main-text a'))
71
            );
72
        });
73
    }
74
75
    /**
76
     * @param Crawler $node
77
     *
78
     * @return Venue
79
     */
80
    private function readVenues(Crawler $node): Venue
81
    {
82
        $venueNode   = $node->filter('.event-detail');
83
        $addressNode = $venueNode->filter('.event-detail-address');
84
85
        $adress = new VenueAddress(
86
            $this->parseString($addressNode->filter('[itemprop="streetAddress"]')),
87
            $this->parseString($addressNode->filter('[itemprop="postalCode"]')),
88
            $this->parseString($addressNode->filter('[itemprop="addressLocality"]')),
89
            $this->parseString($addressNode->filter('[itemprop="addressCountry"]'))
90
        );
91
92
        return new Venue(
93
            $this->parseString($venueNode->filter('[itemprop="name"]')),
94
            $this->parseUrl($venueNode->filter('.event-detail .qa-event-link a')),
95
            $this->parseString($venueNode->filter('.event-detail-tel span')),
96
            $adress
97
        );
98
    }
99
100
    /**
101
     * @param int $id
102
     *
103
     * @return Crawler|null
104
     */
105
    private function crawlEvent(int $id): ?Crawler
106
    {
107
        $url = 'http://www.last.fm/de/event/'.$id;
108
109
        return $this->crawl($url);
110
    }
111
}
112