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

ApiUserController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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