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.

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