Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

UserController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 3
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like $login can also be of type null; however, parameter $login of App\Modules\User\UserRepository::findByLogin() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $item = $userRepository->findByLogin(/** @scrutinizer ignore-type */ $login);
Loading history...
56
        if ($item === null) {
57
            return $responseFactory->createResponse(404);
58
        }
59
60
        return $this->viewRenderer->render('profile', ['item' => $item]);
61
    }
62
}
63