Passed
Pull Request — master (#73)
by Dmitriy
18:21 queued 03:27
created

ApiUserController::profile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace App\Controller;
4
5
use App\DeferredResponse;
6
use App\Entity\User;
7
use App\Repository\UserRepository;
8
use Cycle\ORM\ORMInterface;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Yiisoft\Data\Reader\Sort;
11
use Yiisoft\Factory\Factory;
12
use Psr\Http\Message\ResponseInterface;
13
14
class ApiUserController
15
{
16
    public function index(ORMInterface $orm, Factory $factory): ResponseInterface
17
    {
18
        /** @var UserRepository $userRepo */
19
        $userRepo = $orm->getRepository(User::class);
20
21
        $dataReader = $userRepo->findAll()->withSort((new Sort([]))->withOrderString('login'));
22
        $users = $dataReader->read();
23
24
        $items = [];
25
        foreach ($users as $user) {
26
            $items[] = ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')];
27
        }
28
29
        return $factory->create(DeferredResponse::class, [$items]);
30
    }
31
32
    public function profile(Request $request, ORMInterface $orm, Factory $factory): ResponseInterface
33
    {
34
        /** @var UserRepository $userRepo */
35
        $userRepo = $orm->getRepository(User::class);
36
        $login = $request->getAttribute('login', null);
37
38
        $item = $userRepo->findByLogin($login);
39
        if ($item === null) {
40
            return $factory->create(DeferredResponse::class, [['error' => 'Page not found']])->withStatus(404);
41
        }
42
43
        return $factory->create(
44
            DeferredResponse::class,
45
            [['login' => $item->getLogin(), 'created_at' => $item->getCreatedAt()->format('H:i:s d.m.Y')]]
0 ignored issues
show
Bug introduced by
The method getLogin() does not exist on Yiisoft\Auth\IdentityInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityInterface such as App\Entity\User. ( Ignorable by Annotation )

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

45
            [['login' => $item->/** @scrutinizer ignore-call */ getLogin(), 'created_at' => $item->getCreatedAt()->format('H:i:s d.m.Y')]]
Loading history...
Bug introduced by
The method getCreatedAt() does not exist on Yiisoft\Auth\IdentityInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityInterface such as App\Entity\User. ( Ignorable by Annotation )

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

45
            [['login' => $item->getLogin(), 'created_at' => $item->/** @scrutinizer ignore-call */ getCreatedAt()->format('H:i:s d.m.Y')]]
Loading history...
46
        );
47
    }
48
}
49