Passed
Pull Request — master (#186)
by Alexander
04:39
created

UserController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 32
ccs 10
cts 16
cp 0.625
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 10 1
A __construct() 0 3 1
A profile() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Yiisoft\Data\Paginator\OffsetPaginator;
11
use Yiisoft\Data\Reader\Sort;
12
use Yiisoft\Yii\View\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\ViewRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class UserController
15
{
16
    private const PAGINATION_INDEX = 5;
17
18
    private ViewRenderer $viewRenderer;
19
20 1
    public function __construct(ViewRenderer $viewRenderer)
21
    {
22 1
        $this->viewRenderer = $viewRenderer->withControllerName('user');
23 1
    }
24
25 1
    public function index(Request $request, UserRepository $userRepository): Response
26
    {
27 1
        $pageNum = (int)$request->getAttribute('page', 1);
28
29 1
        $dataReader = $userRepository->findAll()->withSort((new Sort([]))->withOrderString('login'));
30 1
        $paginator = (new OffsetPaginator($dataReader))
31 1
            ->withPageSize(self::PAGINATION_INDEX)
32 1
            ->withCurrentPage($pageNum);
33
34 1
        return $this->viewRenderer->render('index', ['paginator' => $paginator]);
35
    }
36
37
    public function profile(Request $request, UserRepository $userRepository, ResponseFactoryInterface $responseFactory): Response
38
    {
39
        $login = $request->getAttribute('login', null);
40
        $item = $userRepository->findByLogin($login);
41
        if ($item === null) {
42
            return $responseFactory->createResponse(404);
43
        }
44
45
        return $this->viewRenderer->render('profile', ['item' => $item]);
46
    }
47
}
48