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

ApiUserController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
eloc 17
c 3
b 1
f 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A index() 0 14 2
A profile() 0 14 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($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('Page not found', 404);
49
        }
50
51
        return $this->responseFactory->createResponse(
52
            ['login' => $user->getLogin(), 'created_at' => $user->getCreatedAt()->format('H:i:s d.m.Y')]
53
        );
54
    }
55
}
56