Test Failed
Push — develop ( e90cf2...833877 )
by nguereza
02:19
created

DetailAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Platine\Framework\Demo\Action\User;
4
5
use Platine\Framework\Demo\Repository\UserRepository;
6
use Platine\Framework\Demo\Response\RedirectResponse;
7
use Platine\Framework\Demo\Response\TemplateResponse;
8
use Platine\Http\Handler\RequestHandlerInterface;
9
use Platine\Http\ResponseInterface;
10
use Platine\Http\ServerRequestInterface;
11
use Platine\Logger\LoggerInterface;
12
use Platine\Template\Template;
13
14
/**
15
 * Description of DetailAction
16
 *
17
 * @author tony
18
 */
19
class DetailAction implements RequestHandlerInterface
20
{
21
22
    protected LoggerInterface $logger;
23
    protected UserRepository $userRepository;
24
    protected Template $template;
25
26
27
    public function __construct(
28
        LoggerInterface $logger,
29
        Template $template,
30
        UserRepository $userRepository
31
    ) {
32
        $this->logger = $logger;
33
        $this->userRepository = $userRepository;
34
        $this->template = $template;
35
    }
36
37
    public function handle(ServerRequestInterface $request): ResponseInterface
38
    {
39
        $id = (int) $request->getAttribute('id');
40
        $user = $this->userRepository->find($id);
41
        if (!$user) {
42
            $this->logger->warning('Can not find user with id {id}', ['id' => $id]);
43
44
            return (new RedirectResponse('../list'))->redirect();
45
        }
46
47
        return new TemplateResponse(
48
            $this->template,
49
            'detail',
50
            [
51
                'user' => $user,
52
            ]
53
        );
54
    }
55
}
56