1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller; |
6
|
|
|
use App\Entity\User; |
7
|
|
|
use App\Pagination\PaginationSet; |
8
|
|
|
use App\Repository\UserRepository; |
9
|
|
|
use Cycle\ORM\ORMInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
12
|
|
|
use Yiisoft\Data\Paginator\OffsetPaginator; |
13
|
|
|
use Yiisoft\Data\Reader\Sort; |
14
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
15
|
|
|
|
16
|
|
|
class UserController extends Controller |
17
|
|
|
{ |
18
|
|
|
private const PAGINATION_INDEX = 5; |
19
|
|
|
|
20
|
|
|
protected function getId(): string |
21
|
|
|
{ |
22
|
|
|
return 'user'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function index( |
26
|
|
|
Request $request, |
27
|
|
|
ORMInterface $orm, |
28
|
|
|
UrlGeneratorInterface $urlGenerator |
29
|
|
|
): Response { |
30
|
|
|
$pageNum = (int)$request->getAttribute('page', 1); |
31
|
|
|
$response = $this->responseFactory->createResponse(); |
32
|
|
|
/** @var UserRepository $repository */ |
33
|
|
|
$repository = $orm->getRepository(User::class); |
34
|
|
|
|
35
|
|
|
$dataReader = $repository->findAll()->withSort((new Sort([]))->withOrderString('login')); |
36
|
|
|
$paginationSet = new PaginationSet( |
|
|
|
|
37
|
|
|
(new OffsetPaginator($dataReader))->withPageSize(self::PAGINATION_INDEX)->withCurrentPage($pageNum), |
38
|
|
|
fn ($page) => $urlGenerator->generate('user/index', ['page' => $page]) |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$data = [ |
42
|
|
|
'paginationSet' => $paginationSet, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$output = $this->render(__FUNCTION__, $data); |
46
|
|
|
|
47
|
|
|
$response->getBody()->write($output); |
48
|
|
|
return $response; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function profile(Request $request, ORMInterface $orm): Response |
52
|
|
|
{ |
53
|
|
|
$userRepo = $orm->getRepository(User::class); |
54
|
|
|
$login = $request->getAttribute('login', null); |
55
|
|
|
|
56
|
|
|
$item = $userRepo->findByLogin($login); |
|
|
|
|
57
|
|
|
if ($item === null) { |
58
|
|
|
return $this->responseFactory->createResponse(404); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$data = [ |
62
|
|
|
'item' => $item, |
63
|
|
|
]; |
64
|
|
|
$response = $this->responseFactory->createResponse(); |
65
|
|
|
|
66
|
|
|
$output = $this->render('profile', $data); |
67
|
|
|
$response->getBody()->write($output); |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|