Passed
Push — master ( 38c082...bc7b32 )
by Mattia
04:01
created

WebsiteController::userWithUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers;
6
7
use App\Core as MinepicCore;
8
use App\Misc\SplashMessage;
9
use App\Models\AccountStats;
10
use App\Repositories\AccountStatsRepository;
11
use App\Resolvers\UsernameResolver;
12
use Carbon\Carbon;
13
use Illuminate\Http\Response;
14
use Laravel\Lumen\Http\ResponseFactory;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
18
/**
19
 * Class WebsiteController.
20
 */
21
class WebsiteController extends BaseController
22
{
23
    /**
24
     * Default title.
25
     *
26
     * @var string
27
     */
28
    private const DEFAULT_PAGE_TITLE = 'Minecraft avatar generator - Minepic';
29
30
    /**
31
     * Default description.
32
     *
33
     * @var string
34
     */
35
    private const DEFAULT_PAGE_DESCRIPTION = 'MinePic is a free Minecraft avatar and skin viewer '.
36
    'that allow users and developers to pick them for their projects';
37
38
    /**
39
     * Default keywords.
40
     *
41
     * @var string
42
     */
43
    private const DEFAULT_PAGE_KEYWORDS = 'Minecraft, Minecraft avatar viewer, pic, minepic avatar viewer, skin, '.
44
    'minecraft skin, avatar, minecraft avatar, generator, skin generator, skin viewer';
45
46
    /**
47
     * @var ResponseFactory
48
     */
49
    private ResponseFactory $responseFactory;
50
    /**
51
     * @var AccountStatsRepository
52
     */
53
    private AccountStatsRepository $accountStatsRepository;
54
    /**
55
     * @var MinepicCore
56
     */
57
    private MinepicCore $minepicCore;
58
    /**
59
     * @var UsernameResolver
60
     */
61
    private UsernameResolver $usernameResolver;
62
63
    /**
64
     * WebsiteController constructor.
65
     *
66
     * @param AccountStatsRepository $accountStatsRepository
67
     * @param MinepicCore            $minepicCore
68
     * @param ResponseFactory        $responseFactory
69
     */
70
    public function __construct(
71
        AccountStatsRepository $accountStatsRepository,
72
        MinepicCore $minepicCore,
73
        ResponseFactory $responseFactory,
74
        UsernameResolver $usernameResolver
75
    ) {
76
        $this->responseFactory = $responseFactory;
77
        $this->accountStatsRepository = $accountStatsRepository;
78
        $this->minepicCore = $minepicCore;
79
        $this->usernameResolver = $usernameResolver;
80
    }
81
82
    /**
83
     * Compose view with header and footer.
84
     *
85
     * @param string $page
86
     * @param array  $bodyData
87
     * @param array  $headerData
88
     *
89
     * @return string
90
     */
91
    private function composeView(
92
        string $page = '',
93
        array $bodyData = [],
94
        array $headerData = []
95
    ): string {
96
        return view('public.template.header', $headerData).
97
            view('public.'.$page, $bodyData).
98
            view('public.template.footer');
99
    }
100
101
    /**
102
     * Render fullpage (headers, body, footer).
103
     *
104
     * @param string $page
105
     * @param array  $bodyData
106
     * @param array  $headerData
107
     *
108
     * @return Response
109
     */
110
    private function renderPage(
111
        string $page = '',
112
        array $bodyData = [],
113
        array $headerData = []
114
    ): Response {
115
        $realHeaderData = [];
116
        $realHeaderData['title'] = $headerData['title'] ?? self::DEFAULT_PAGE_TITLE;
117
        $realHeaderData['description'] = $headerData['description'] ?? self::DEFAULT_PAGE_DESCRIPTION;
118
        $realHeaderData['keywords'] = $headerData['keywords'] ?? self::DEFAULT_PAGE_KEYWORDS;
119
        $realHeaderData['randomMessage'] = SplashMessage::get();
120
121
        $view = $this->composeView($page, $bodyData, $realHeaderData);
122
123
        return $this->responseFactory->make(
124
            $view,
125
            Response::HTTP_OK
126
        );
127
    }
128
129
    /**
130
     * Index.
131
     */
132
    public function index(): Response
133
    {
134
        $bodyData = [
135
            'lastRequests' => AccountStats::getLastUsers(),
136
            'mostWanted' => AccountStats::getMostWanted(),
137
        ];
138
139
        return $this->renderPage('index', $bodyData);
140
    }
141
142
    /**
143
     * User stats page.
144
     *
145
     * @param string $uuidOrName
146
     *
147
     * @throws \Exception
148
     *
149
     * @return Response
150
     */
151
    public function user(string $uuid): Response
152
    {
153
        if ($this->minepicCore->initialize($uuid)) {
154
            $userdata = $this->minepicCore->getUserdata();
155
            $userstats = $this->accountStatsRepository->findByUuid($userdata->uuid);
156
157
            $headerData = [
158
                'title' => $userdata->username.' usage statistics - Minepic',
159
                'description' => 'MinePic usage statistics for the user '.$userdata->username,
160
                'keywords' => 'Minecraft, Minecraft avatar viewer, pic, minepic avatar viewer, skin, '.
161
                    'minecraft skin, avatar, minecraft avatar, generator, skin generator, skin viewer',
162
            ];
163
164
            $bodyData = [
165
                'user' => [
166
                    'uuid' => $userdata->uuid,
167
                    'username' => $userdata->username,
168
                    'count_request' => $userstats->count_request,
169
                    'count_search' => $userstats->count_search,
170
                    'last_request' => Carbon::createFromTimestamp($userstats->time_request)->format(Carbon::ATOM),
171
                    'last_search' => Carbon::createFromTimestamp($userstats->time_search)->format(Carbon::ATOM),
172
                ],
173
            ];
174
175
            return $this->renderPage('user', $bodyData, $headerData);
176
        }
177
178
        throw new NotFoundHttpException();
179
    }
180
181
    /**
182
     * @param string $username
183
     *
184
     * @throws \Exception
185
     *
186
     * @return Response
187
     */
188
    public function userWithUsername(string $username)
189
    {
190
        $uuid = $this->usernameResolver->resolve($username);
191
        if ($uuid === env('DEFAULT_UUID')) {
0 ignored issues
show
introduced by
The condition $uuid === env('DEFAULT_UUID') is always false.
Loading history...
192
            throw new NotFoundHttpException();
193
        }
194
195
        return $this->user($uuid);
196
    }
197
}
198