Passed
Pull Request — master (#49)
by
unknown
14:54
created

UserController::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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(
0 ignored issues
show
Deprecated Code introduced by
The class App\Pagination\PaginationSet has been deprecated: Should be replaced to a Pagination Widget ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

36
        $paginationSet = /** @scrutinizer ignore-deprecated */ new PaginationSet(
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method findByLogin() does not exist on Cycle\ORM\RepositoryInterface. It seems like you code against a sub-type of Cycle\ORM\RepositoryInterface such as App\Repository\UserRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        /** @scrutinizer ignore-call */ 
57
        $item = $userRepo->findByLogin($login);
Loading history...
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