1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use Buzz\Browser; |
8
|
|
|
use App\Http\Requests; |
9
|
|
|
use App\Http\Controllers\Controller; |
10
|
|
|
use MetalMatze\LastFm\LastFm; |
11
|
|
|
|
12
|
|
|
class LastFmController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* LastFm Object |
16
|
|
|
* @var object; |
17
|
|
|
*/ |
18
|
|
|
protected $lastfm; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Initialize the Controller with necessary arguments |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->lastfm = new LastFm(new Browser); |
26
|
|
|
$this->lastfm->setApiKey(env('LASTFM_API_KEY')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return all tweets to the Twitter API dashboard |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function getPage() |
34
|
|
|
{ |
35
|
|
|
$details = $this->getArtistInfo(); |
36
|
|
|
|
37
|
|
|
$albums = array_slice($this->getTopAlbums(), 0, 4); |
38
|
|
|
|
39
|
|
|
$tracks = array_slice($this->getTopTracks(), 0, 10); |
40
|
|
|
|
41
|
|
|
return view('api.lastfm')->withDetails($details) |
42
|
|
|
->withAlbums($albums) |
43
|
|
|
->withTracks($tracks); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get Artist Info |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
private function getArtistInfo() |
51
|
|
|
{ |
52
|
|
|
$result = json_decode($this->lastfm->artist_getInfo(['artist' => 'The Pierces']), true); |
53
|
|
|
|
54
|
|
|
return $result['artist']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get Top Albums |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private function getTopAlbums() |
62
|
|
|
{ |
63
|
|
|
$result = json_decode($this->lastfm->artist_getTopAlbums(['artist' => 'The Pierces']), true); |
64
|
|
|
|
65
|
|
|
return $result['topalbums']['album']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Top Tracks |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getTopTracks() |
73
|
|
|
{ |
74
|
|
|
$result = json_decode($this->lastfm->artist_getTopTracks(['artist' => 'The Pierces']), true); |
75
|
|
|
|
76
|
|
|
return $result['toptracks']['track']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|