HomeController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 11 2
B report() 0 43 5
1
<?php
2
namespace MyWonderland\Controller;
3
4
/**
5
 * Created by PhpStorm.
6
 * User: thiago
7
 * Date: 31/03/18
8
 * Time: 20:50
9
 */
10
11
use MyWonderland\Domain\Manager\StorageManager;
12
use MyWonderland\Domain\Model\Artist;
13
use MyWonderland\Domain\Model\Ranking;
14
use MyWonderland\Domain\Model\SpotifyToken;
15
use MyWonderland\Service\SongkickService;
16
use MyWonderland\Service\SpotifyService;
17
18
class HomeController extends AbstractController
19
{
20
    /**
21
     * @var SpotifyService
22
     */
23
    protected $spotifyService;
24
25
    /**
26
     * @var SongkickService
27
     */
28
    protected $songkickService;
29
30
    /**
31
     * HomeController constructor.
32
     * @param StorageManager $storageManager
33
     * @param SpotifyService $spotifyService
34
     * @param SongkickService $songkickService
35
     */
36
    public function __construct(StorageManager $storageManager, \Twig_Environment $twig, SpotifyService $spotifyService, SongkickService $songkickService)
37
    {
38
        parent::__construct($storageManager, $twig);
39
        $this->spotifyService = $spotifyService;
40
        $this->songkickService = $songkickService;
41
    }
42
43
44
    public function index()
45
    {
46
        $this->storeManager->unset('token');
47
        // @todo Implement a middleware
48
        $logged = $this->storeManager->has('token');
49
        if ($logged === true) {
50
            header('Location: /report');
51
        }
52
53
        print $this->twig->render('index.twig', [
54
            'logged' => $logged
55
        ]);
56
    }
57
58
59
    public function report()
60
    {
61
        try {
62
            // @todo Implement a middleware
63
            $logged = $this->storeManager->has('token');
64
            if ($logged !== true) {
65
                header('Location: /');
66
            }
67
68
            /**
69
             * @var SpotifyToken $token
70
             */
71
            $token = $this->storeManager->get('token');
72
            $me = $this->spotifyService->requestMe($token);
73
            $topArtists = $this->spotifyService->requestTopArtists($token);
74
            $events = [];
75
            $count = 0;
76
            foreach ($topArtists as $artist) {
77
                /**
78
                 * @var $artist Artist
79
                 */
80
                $artist->songkickId = $this->songkickService->getArtistIdByName(
81
                    getenv('SONGKICK_API_KEY'), $artist->name);
82
                $artistEvents = $this->songkickService->getArtistUpcomingEvents(
83
                    getenv('SONGKICK_API_KEY'), $artist->songkickId);
84
                if (!empty($artistEvents)) {
85
                    $count += count($artistEvents);
86
                    $events = array_merge($events, $artistEvents);
87
                }
88
            }
89
            $ranking = new Ranking($events);
90
            $report = $ranking->report();
91
92
            print $this->twig->render('report.twig', [
93
                'logged' => $logged,
94
                'me' => $me,
95
                'topArtists' => $topArtists,
96
                'report' => array_slice($report, 0, 10)
97
            ]);
98
        } catch (\Exception $e) {
99
            print $this->twig->render('error.twig', [
100
                'code' => $e->getCode(),
101
                'message' => $e->getMessage()
102
            ]);
103
        }
104
105
    }
106
}