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\SetlistFm\Service; |
13
|
|
|
|
14
|
|
|
use Core23\SetlistFm\Connection\ConnectionInterface; |
15
|
|
|
use Core23\SetlistFm\Exception\ApiException; |
16
|
|
|
use Core23\SetlistFm\Exception\NotFoundException; |
17
|
|
|
use Core23\SetlistFm\Model\Setlist; |
18
|
|
|
use Core23\SetlistFm\Model\User; |
19
|
|
|
|
20
|
|
|
final class UserService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ConnectionInterface |
24
|
|
|
*/ |
25
|
|
|
private $connection; |
26
|
|
|
|
27
|
|
|
public function __construct(ConnectionInterface $connection) |
28
|
|
|
{ |
29
|
|
|
$this->connection = $connection; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the user data for an id. |
34
|
|
|
* |
35
|
|
|
* @throws ApiException |
36
|
|
|
* @throws NotFoundException |
37
|
|
|
*/ |
38
|
|
|
public function getUser(string $userId): User |
39
|
|
|
{ |
40
|
|
|
return User::fromApi( |
41
|
|
|
$this->connection->call('user/'.$userId) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get a list of attended venues for a user id. |
47
|
|
|
* |
48
|
|
|
* @throws ApiException |
49
|
|
|
* @throws NotFoundException |
50
|
|
|
* |
51
|
|
|
* @return Setlist[] |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function getAttends(string $userId, int $page = 1): array |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$response = $this->connection->call('user/'.$userId.'/attended', [ |
56
|
|
|
'p' => $page, |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
if (!\array_key_exists('setlist', $response)) { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return array_map(static function ($data) { |
64
|
|
|
return Setlist::fromApi($data); |
65
|
|
|
}, $response['setlist']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a list of edited venues for a user id. |
70
|
|
|
* |
71
|
|
|
* @throws ApiException |
72
|
|
|
* @throws NotFoundException |
73
|
|
|
* |
74
|
|
|
* @return Setlist[] |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function getEdits(string $userId, int $page = 1): array |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$response = $this->connection->call('user/'.$userId.'/edited', [ |
79
|
|
|
'p' => $page, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
if (!\array_key_exists('setlist', $response)) { |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return array_map(static function ($data) { |
87
|
|
|
return Setlist::fromApi($data); |
88
|
|
|
}, $response['setlist']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.