Passed
Pull Request — master (#412)
by Wilmer
03:49
created

UserController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User\Controller;
6
7
use App\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
    private ViewRenderer $viewRenderer;
21
22 1
    public function __construct(ViewRenderer $viewRenderer)
23
    {
24 1
        $this->viewRenderer = $viewRenderer->withControllerName('user');
25 1
    }
26
27 1
    public function index(
28
        UserRepository $userRepository,
29
        CurrentRoute $currentRoute,
30
        ServerRequestInterface $request
31
    ): Response {
32 1
        $pageNum = (int)$currentRoute->getArgument('page', '1');
33 1
        $sortOrderString = $request->getQueryParams();
34
35 1
        $dataReader = $userRepository
36 1
            ->findAll()
37 1
            ->withSort(Sort::only(['id', 'login'])->withOrderString($sortOrderString['sort'] ?? ''));
38
39 1
        $paginator = (new OffsetPaginator($dataReader));
40
41 1
        return $this->viewRenderer->render(
42 1
            'index',
43 1
            ['currentPage' => $pageNum, 'paginator' => $paginator, 'pageSize' => self::PAGINATION_INDEX]
44
        );
45
    }
46
47
    public function profile(CurrentRoute $currentRoute, UserRepository $userRepository, ResponseFactoryInterface $responseFactory): Response
48
    {
49
        $login = $currentRoute->getArgument('login');
50
        $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\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

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