Passed
Push — dev ( 4bea9d...fc80c1 )
by Mattia
05:53
created

WebsiteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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