Passed
Push — master ( 25046f...2db671 )
by Mattia
03:56
created

JsonController::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
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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\Models\AccountStats;
9
use App\Repositories\AccountRepository;
10
use App\Resolvers\UsernameResolver;
11
use App\Transformers\Account\AccountBasicDataTransformer;
12
use App\Transformers\Account\AccountTypeaheadTransformer;
13
use Illuminate\Http\JsonResponse;
14
use Laravel\Lumen\Http\ResponseFactory;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
use League\Fractal;
17
use League\Fractal\Manager;
18
use League\Fractal\Serializer\ArraySerializer;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
class JsonController extends BaseController
22
{
23
    /**
24
     * @var ResponseFactory
25
     */
26
    private ResponseFactory $responseFactory;
27
    /**
28
     * @var MinepicCore
29
     */
30
    private MinepicCore $minepicCore;
31
    /**
32
     * @var AccountRepository
33
     */
34
    private AccountRepository $accountRepository;
35
    /**
36
     * @var Manager
37
     */
38
    private Manager $dataManger;
39
    /**
40
     * @var UsernameResolver
41
     */
42
    private UsernameResolver $usernameResolver;
43
44
    /**
45
     * JsonController constructor.
46
     *
47
     * @param AccountRepository $accountRepository
48
     * @param MinepicCore       $minepicCore
49
     * @param Manager           $dataManger
50
     * @param ResponseFactory   $responseFactory
51
     * @param UsernameResolver  $usernameResolver
52
     */
53
    public function __construct(
54
        AccountRepository $accountRepository,
55
        MinepicCore $minepicCore,
56
        Manager $dataManger,
57
        ResponseFactory $responseFactory,
58
        UsernameResolver $usernameResolver
59
    ) {
60
        $this->accountRepository = $accountRepository;
61
        $this->minepicCore = $minepicCore;
62
        $this->dataManger = $dataManger;
63
        $this->responseFactory = $responseFactory;
64
        $this->usernameResolver = $usernameResolver;
65
66
        $this->dataManger->setSerializer(new ArraySerializer());
67
    }
68
69
    /**
70
     * User info.
71
     *
72
     * @param string $uuid
73
     *
74
     * @throws \Exception
75
     *
76
     * @return JsonResponse
77
     */
78
    public function user($uuid = ''): JsonResponse
79
    {
80
        if (!$this->minepicCore->initialize($uuid)) {
81
            $httpStatus = 404;
82
            $response = [
83
                'ok' => false,
84
                'message' => 'User not found',
85
            ];
86
87
            return $this->responseFactory->json($response, $httpStatus);
88
        }
89
90
        $httpStatus = 200;
91
        $account = $this->minepicCore->getUserdata();
92
93
        if ($account === null) {
94
            throw new NotFoundHttpException();
95
        }
96
        $resource = new Fractal\Resource\Item($account, new AccountBasicDataTransformer());
97
98
        $response = [
99
            'ok' => true,
100
            'data' => $this->dataManger->createData($resource)->toArray(),
101
        ];
102
103
        return $this->responseFactory->json($response, $httpStatus);
104
    }
105
106
    /**
107
     * @param string $username
108
     *
109
     * @throws \Exception
110
     *
111
     * @return JsonResponse
112
     */
113
    public function userWithUsername(string $username): JsonResponse
114
    {
115
        $uuid = $this->usernameResolver->resolve($username);
116
        if ($uuid === env('DEFAULT_UUID')) {
0 ignored issues
show
introduced by
The condition $uuid === env('DEFAULT_UUID') is always false.
Loading history...
117
            throw new NotFoundHttpException();
118
        }
119
120
        return $this->user($uuid);
121
    }
122
123
    /**
124
     * Update User data.
125
     *
126
     * @param string $uuidOrName
127
     *
128
     * @throws \Exception
129
     *
130
     * @return JsonResponse
131
     */
132
    public function updateUser(string $uuidOrName): JsonResponse
133
    {
134
        // Force user update
135
        $this->minepicCore->setForceUpdate(true);
136
137
        // Check if user exists
138
        if ($this->minepicCore->initialize($uuidOrName)) {
139
            // Check if data has been updated
140
            if ($this->minepicCore->userDataUpdated()) {
141
                $response = ['ok' => true, 'message' => 'Data updated'];
142
                $httpStatus = 200;
143
            } else {
144
                $userdata = $this->minepicCore->getUserdata();
145
                $dateString = $userdata->updated_at->toW3cString();
146
147
                $response = [
148
                    'ok' => false,
149
                    'message' => 'Cannot update user, userdata has been updated recently',
150
                    'last_update' => $dateString,
151
                ];
152
153
                $httpStatus = 403;
154
            }
155
        } else {
156
            $response = ['ok' => false, 'message' => 'User not found'];
157
            $httpStatus = 404;
158
        }
159
160
        return $this->responseFactory->json($response, $httpStatus);
161
    }
162
163
    /**
164
     * Username Typeahead.
165
     *
166
     * @param $term
167
     *
168
     * @return JsonResponse
169
     */
170
    public function userTypeahead($term): JsonResponse
171
    {
172
        $accountsPagination = $this->accountRepository->filterPaginate(['term' => $term], 15);
173
174
        $resource = new Fractal\Resource\Collection(
175
            $accountsPagination->items(),
176
            new AccountTypeaheadTransformer()
177
        );
178
179
        return $this->responseFactory->json(
180
            $this->dataManger->createData($resource)->toArray()
181
        );
182
    }
183
184
    /**
185
     * Get most wanted account list.
186
     */
187
    public function getMostWantedUsers(): JsonResponse
188
    {
189
        return $this->responseFactory->json([
190
            'ok' => true,
191
            'data' => AccountStats::getMostWanted(),
192
        ]);
193
    }
194
}
195