|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Http\Controllers\User; |
|
6
|
|
|
|
|
7
|
|
|
use App\Modules\User\UserRepository; |
|
8
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Yiisoft\Data\Paginator\OffsetPaginator; |
|
12
|
|
|
use Yiisoft\Data\Reader\Sort; |
|
13
|
|
|
use Yiisoft\Router\CurrentRoute; |
|
14
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
15
|
|
|
|
|
16
|
|
|
final class UserController |
|
17
|
|
|
{ |
|
18
|
|
|
private const PAGINATION_INDEX = 5; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(private ViewRenderer $viewRenderer) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('user'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function index( |
|
26
|
|
|
CurrentRoute $currentRoute, |
|
27
|
|
|
ServerRequestInterface $request, |
|
28
|
|
|
UserRepository $userRepository |
|
29
|
|
|
): Response { |
|
30
|
|
|
$page = (int)$currentRoute->getArgument('page', '1'); |
|
31
|
|
|
$sortOrderString = $request->getQueryParams(); |
|
32
|
|
|
|
|
33
|
|
|
$dataReader = $userRepository |
|
34
|
|
|
->findAll() |
|
35
|
|
|
->withSort(Sort::only(['id', 'login'])->withOrderString($sortOrderString['sort'] ?? '')); |
|
36
|
|
|
|
|
37
|
|
|
$paginator = (new OffsetPaginator($dataReader))->withPageSize(self::PAGINATION_INDEX); |
|
38
|
|
|
|
|
39
|
|
|
return $this->viewRenderer->render( |
|
40
|
|
|
'index', |
|
41
|
|
|
[ |
|
42
|
|
|
'page' => $page, |
|
43
|
|
|
'paginator' => $paginator, |
|
44
|
|
|
'sortOrder' => $sortOrderString['sort'] ?? '', |
|
45
|
|
|
] |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function profile( |
|
50
|
|
|
CurrentRoute $currentRoute, |
|
51
|
|
|
ResponseFactoryInterface $responseFactory, |
|
52
|
|
|
UserRepository $userRepository |
|
53
|
|
|
): Response { |
|
54
|
|
|
$login = $currentRoute->getArgument('login'); |
|
55
|
|
|
$item = $userRepository->findByLogin($login); |
|
|
|
|
|
|
56
|
|
|
if ($item === null) { |
|
57
|
|
|
return $responseFactory->createResponse(404); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->viewRenderer->render('profile', ['item' => $item]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|