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