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 ( 42361c...1e8b43 )
by Christian
15s
created

ArtistEventCrawler::getArtistYears()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
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\Model\Event;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
final class ArtistEventCrawler extends AbstractCrawler implements ArtistEventCrawlerInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getArtistYears(string $artist): array
21
    {
22
        $node = $this->crawlUrl($artist);
23
24
        if (null === $node) {
25
            return [];
26
        }
27
28
        $years = $node->filter('.content-top .secondary-nav-item-link')
29
            ->each(static function (Crawler $node) {
30
                return (int) trim($node->text());
31
            })
32
        ;
33
34
        sort($years);
35
        array_shift($years);
36
37
        return $years;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getEvents(string $artist, ?int $year, int $page = 1): array
44
    {
45
        $node = $this->crawlUrl($artist, $year, $page);
46
47
        if (null === $node) {
48
            return [];
49
        }
50
51
        return $node->filter('.events-list-item')->each(function (Crawler $node): Event {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
52
            return $this->parseEvent($node);
53
        });
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getYearPages(string $artist, ?int $year): int
60
    {
61
        $node = $this->crawlUrl($artist, $year);
62
63
        if (null === $node) {
64
            return 0;
65
        }
66
67
        return $this->countListPages($node);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getYearCount(string $artist, ?int $year, int $page = 1): int
74
    {
75
        $node = $this->crawlUrl($artist, $year, $page);
76
77
        if (null === $node) {
78
            return 0;
79
        }
80
81
        $perPage = $this->countListEvents($node);
82
        $pages   = $this->countListPages($node);
83
84
        if ($pages) {
85
            $node = $this->crawlUrl($artist, $year, $pages);
86
87
            if (null === $node) {
88
                return $perPage;
89
            }
90
91
            $count = $this->countListEvents($node);
92
93
            return ($pages - 1) * $perPage + $count;
94
        }
95
96
        return $perPage;
97
    }
98
99
    /**
100
     * @param Crawler $node
101
     *
102
     * @return int
103
     */
104
    private function countListPages(Crawler $node): int
105
    {
106
        $pagination = $this->parseString($node->filter('.pagination .pagination-page')->last());
107
108
        return $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1;
109
    }
110
111
    /**
112
     * @param Crawler $node
113
     *
114
     * @return int
115
     */
116
    private function countListEvents(Crawler $node): int
117
    {
118
        return $node->filter('.events-list-item')->count();
119
    }
120
121
    /**
122
     * @param string   $artist
123
     * @param int|null $year
124
     * @param int      $page
125
     *
126
     * @return Crawler|null
127
     */
128
    private function crawlUrl(string $artist, ?int $year = null, int $page = 1): ?Crawler
129
    {
130
        $url = 'https://www.last.fm/artist/'.$artist.'/+events/'.($year ?: '');
131
132
        return $this->crawl($url, [
133
            'page' => $page,
134
        ]);
135
    }
136
}
137