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 ( d20c5f...f10e39 )
by Christian
07:00
created

EventListCrawler::getYearCount()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 13
nc 4
nop 3
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 Symfony\Component\DomCrawler\Crawler;
13
14
final class EventListCrawler extends AbstractCrawler
15
{
16
    /**
17
     * @param string $username
18
     *
19
     * @return int[]|null
20
     */
21
    public function getUserYears(string $username): ? array
22
    {
23
        $node = $this->crawlEventList($username);
24
25
        if (null === $node) {
26
            return null;
27
        }
28
29
        $years = $node->filter('.content-top .secondary-nav-item-link')
30
            ->each(function (Crawler $node) {
31
                return (int) trim($node->text());
32
            });
33
34
        sort($years);
35
        array_shift($years);
36
37
        return $years;
38
    }
39
40
    /**
41
     * Get all events of a user.
42
     *
43
     * @param string   $username
44
     * @param int|null $year
45
     * @param int      $page
46
     *
47
     * @return array|null
48
     */
49
    public function getEvents(string $username, ? int $year, int $page = 1): ? array
50
    {
51
        $node = $this->crawlEventList($username, $year, $page);
52
53
        if (null === $node) {
54
            return null;
55
        }
56
57
        return $node->filter('.events-list-item')->each(function (Crawler $node): array {
58
            $eventNode = $node->filter('.events-list-item-event--title a');
59
60
            $url = $this->parseUrl($eventNode);
61
            $id = preg_replace('/.*\/(\d+)+.*/', '$1', $url);
62
63
            return array(
64
                'eventId' => (int) $id,
65
                'title'   => $this->parseString($eventNode),
66
                'time'    => new \DateTime($node->filter('time')->attr('datetime')),
67
                'url'     => $url,
68
            );
69
        });
70
    }
71
72
    /**
73
     * Gets the pages for a year.
74
     *
75
     * @param string   $username
76
     * @param int|null $year
77
     *
78
     * @return int|null
79
     */
80
    public function getYearPages(string $username, ? int $year): ? int
81
    {
82
        $node = $this->crawlEventList($username, $year);
83
84
        if (null === $node) {
85
            return null;
86
        }
87
88
        return $this->countListPages($node);
89
    }
90
91
    /**
92
     * Gets the event count for a year.
93
     *
94
     * @param string   $username
95
     * @param int|null $year
96
     * @param int      $page
97
     *
98
     * @return int|null
99
     */
100
    public function getYearCount(string $username, ? int $year, int $page = 1): ? int
101
    {
102
        $node = $this->crawlEventList($username, $year, $page);
103
104
        if (null === $node) {
105
            return null;
106
        }
107
108
        $perPage = $this->countListEvents($node);
109
        $pages   = $this->countListPages($node);
110
111
        if ($pages) {
112
            $node  = $this->crawlEventList($username, $year, $pages);
113
114
            if (null === $node) {
115
                return $perPage;
116
            }
117
118
            $count = $this->countListEvents($node);
119
120
            return ($pages - 1) * $perPage + $count;
121
        }
122
123
        return $perPage;
124
    }
125
126
    /**
127
     * @param Crawler $node
128
     *
129
     * @return int
130
     */
131
    private function countListPages(Crawler $node): int
132
    {
133
        $pagination = $this->parseString($node->filter('.pagination .pages'));
134
135
        return $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1;
136
    }
137
138
    /**
139
     * @param Crawler $node
140
     *
141
     * @return int
142
     */
143
    private function countListEvents(Crawler $node): int
144
    {
145
        return $node->filter('.events-list-item')->count();
146
    }
147
148
    /**
149
     * @param string   $username
150
     * @param int|null $year
151
     * @param int      $page
152
     *
153
     * @return Crawler|null
154
     */
155
    private function crawlEventList(string $username, ? int $year = null, int $page = 1): ? Crawler
156
    {
157
        $url = 'http://www.last.fm/user/'.$username.'/events/'.($year ?: '').'?page='.$page;
158
159
        return $this->crawl($url);
160
    }
161
}
162