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

ApiUserController::profile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 9
c 2
b 1
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\DeferredResponseFactory;
0 ignored issues
show
Bug introduced by
The type App\DeferredResponseFactory 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...
6
use App\Entity\User;
7
use App\Repository\UserRepository;
8
use Cycle\ORM\ORMInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Yiisoft\Data\Reader\Sort;
12
use Yiisoft\Yii\Web\WebResponseFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\WebResponseFactoryInterface 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 ApiUserController
15
{
16
    private WebResponseFactoryInterface $responseFactory;
17
18
    public function __construct(WebResponseFactoryInterface $responseFactory)
19
    {
20
        $this->responseFactory = $responseFactory;
21
    }
22
23
    public function index(ORMInterface $orm): ResponseInterface
24
    {
25
        /** @var UserRepository $userRepo */
26
        $userRepo = $orm->getRepository(User::class);
27
28
        $dataReader = $userRepo->findAll()->withSort((new Sort([]))->withOrderString('login'));
29
        $users = $dataReader->read();
30
31
        $items = [];
32
        foreach ($users as $user) {
33
            $items[] = ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')];
34
        }
35
36
        return $this->responseFactory->createResponse(200, '', $items);
37
    }
38
39
    public function profile(Request $request, ORMInterface $orm): ResponseInterface
40
    {
41
        /** @var UserRepository $userRepository */
42
        $userRepository = $orm->getRepository(User::class);
43
        $login = $request->getAttribute('login', null);
44
45
        /** @var User $user */
46
        $user = $userRepository->findByLogin($login);
47
        if ($user === null) {
48
            return $this->responseFactory->createResponse(404, '', 'Page not found');
49
        }
50
51
        return $this->responseFactory->createResponse(
52
            200,
53
            '',
54
            ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')]
55
        );
56
    }
57
}
58