Passed
Pull Request — master (#73)
by Dmitriy
14:14
created

ApiUserController::profile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
namespace App\Controller;
4
5
use App\DeferredResponse;
6
use App\DeferredResponseFactory;
7
use App\Entity\User;
8
use App\Repository\UserRepository;
9
use Cycle\ORM\ORMInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Yiisoft\Data\Reader\Sort;
12
use Yiisoft\Factory\Factory;
13
use Psr\Http\Message\ResponseInterface;
14
15
class ApiUserController
16
{
17
    private DeferredResponseFactory $responseFactory;
18
19
    public function __construct(DeferredResponseFactory $responseFactory)
20
    {
21
        $this->responseFactory = $responseFactory;
22
    }
23
24
    public function index(ORMInterface $orm, Factory $factory): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $factory 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

24
    public function index(ORMInterface $orm, /** @scrutinizer ignore-unused */ Factory $factory): ResponseInterface

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...
25
    {
26
        /** @var UserRepository $userRepo */
27
        $userRepo = $orm->getRepository(User::class);
28
29
        $dataReader = $userRepo->findAll()->withSort((new Sort([]))->withOrderString('login'));
30
        $users = $dataReader->read();
31
32
        $items = [];
33
        foreach ($users as $user) {
34
            $items[] = ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')];
35
        }
36
37
        return $this->responseFactory->createResponse()->withData($items);
38
    }
39
40
    public function profile(Request $request, ORMInterface $orm, Factory $factory): ResponseInterface
41
    {
42
        /** @var UserRepository $userRepository */
43
        $userRepository = $orm->getRepository(User::class);
44
        $login = $request->getAttribute('login', null);
45
46
        /** @var User $user */
47
        $user = $userRepository->findByLogin($login);
48
        if ($user === null) {
49
            return $factory->create(DeferredResponse::class, [['error' => 'Page not found']])->withStatus(404);
50
        }
51
52
        return $this->responseFactory->createResponse()->withData(
53
            ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')]
54
        );
55
    }
56
}
57