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.

ArtistEventCrawler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 89
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getArtistYears() 0 19 2
A getEvents() 0 12 2
A getYearPages() 0 10 2
A getYearCount() 0 21 3
A countListPages() 0 6 2
A countListEvents() 0 4 1
A crawlUrl() 0 8 2
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\Model\Event;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
final class ArtistEventCrawler extends AbstractCrawler implements ArtistEventCrawlerInterface
16
{
17
    public function getArtistYears(string $artist): array
18
    {
19
        $node = $this->crawlUrl($artist);
20
21
        if (null === $node) {
22
            return [];
23
        }
24
25
        $years = $node->filter('.content-top .secondary-nav-item-link')
26
            ->each(static function (Crawler $node) {
27
                return (int) trim($node->text());
28
            })
29
        ;
30
31
        sort($years);
32
        array_shift($years);
33
34
        return $years;
35
    }
36
37
    public function getEvents(string $artist, ?int $year, int $page = 1): array
38
    {
39
        $node = $this->crawlUrl($artist, $year, $page);
40
41
        if (null === $node) {
42
            return [];
43
        }
44
45
        return $node->filter('.events-list-item')->each(function (Crawler $node): Event {
46
            return $this->parseEvent($node);
47
        });
48
    }
49
50
    public function getYearPages(string $artist, ?int $year): int
51
    {
52
        $node = $this->crawlUrl($artist, $year);
53
54
        if (null === $node) {
55
            return 0;
56
        }
57
58
        return $this->countListPages($node);
59
    }
60
61
    public function getYearCount(string $artist, ?int $year, int $page = 1): int
62
    {
63
        $node = $this->crawlUrl($artist, $year, $page);
64
65
        if (null === $node) {
66
            return 0;
67
        }
68
69
        $perPage = $this->countListEvents($node);
70
        $pages   = $this->countListPages($node);
71
72
        $node = $this->crawlUrl($artist, $year, $pages);
73
74
        if (null === $node) {
75
            return $perPage;
76
        }
77
78
        $count = $this->countListEvents($node);
79
80
        return ($pages - 1) * $perPage + $count;
81
    }
82
83
    private function countListPages(Crawler $node): int
84
    {
85
        $pagination = $this->parseString($node->filter('.pagination .pagination-page')->last());
86
87
        return null !== $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1;
88
    }
89
90
    private function countListEvents(Crawler $node): int
91
    {
92
        return $node->filter('.events-list-item')->count();
93
    }
94
95
    private function crawlUrl(string $artist, ?int $year = null, int $page = 1): ?Crawler
96
    {
97
        $url = 'https://www.last.fm/artist/'.$artist.'/+events/'.($year ?: '');
98
99
        return $this->crawl($url, [
100
            'page' => $page,
101
        ]);
102
    }
103
}
104