Passed
Pull Request — master (#49)
by
unknown
17:14 queued 02:12
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\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
0 ignored issues
show
Unused Code introduced by
The parameter $urlGenerator is not used and could be removed. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-unused */ UrlGeneratorInterface $urlGenerator

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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(__FUNCTION__, $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);
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

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