Test Failed
Push — develop ( 2de2bf...7fb7cc )
by nguereza
02:31
created

MyRequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 16
rs 10
1
<?php
2
3
namespace Platine\Framework\Demo;
4
5
use Platine\Config\Config;
6
use Platine\Cookie\Cookie;
7
use Platine\Cookie\CookieManagerInterface;
8
use Platine\Framework\App\Application;
9
use Platine\Framework\Demo\Repository\UserRepository;
10
use Platine\Http\Handler\RequestHandlerInterface;
11
use Platine\Http\Response;
12
use Platine\Http\ResponseInterface;
13
use Platine\Http\ServerRequestInterface;
14
use Platine\Logger\LoggerInterface;
15
use Platine\Session\Session;
16
use Platine\Template\Template;
17
18
/**
19
 * Description of MyRequestHandler
20
 *
21
 * @author tony
22
 */
23
class MyRequestHandler implements RequestHandlerInterface
24
{
25
26
    protected LoggerInterface $logger;
27
    protected Application $app;
28
    protected Config $config;
29
    protected Session $session;
30
    protected UserRepository $userRepository;
31
    protected Template $template;
32
    protected CookieManagerInterface $cookieManager;
33
34
35
    public function __construct(
36
        LoggerInterface $logger,
37
        Application $app,
38
        Config $config,
39
        Session $session,
40
        Template $template,
41
        UserRepository $userRepository,
42
        CookieManagerInterface $cookieManager
43
    ) {
44
        $this->logger = $logger;
45
        $this->app = $app;
46
        $this->config = $config;
47
        $this->session = $session;
48
        $this->userRepository = $userRepository;
49
        $this->template = $template;
50
        $this->cookieManager = $cookieManager;
51
    }
52
53
    public function handle(ServerRequestInterface $request): ResponseInterface
54
    {
55
56
        //$param = new RequestData($request);
57
58
        if (!$this->session->has('lang')) {
59
            $this->logger->info('Session language not exist create it now');
60
            $this->session->set('lang', 'fr_Fr');
61
        }
62
        $lang = $this->session->get('lang');
63
64
        $name = $request->getAttribute('name', 'Tony');
65
        $user = $this->userRepository->find(1);
66
        //$this->logger->info('User is {user}', ['user' => print_r($user, true)]);
67
        //$this->cookieManager->add(new Cookie('app_user', print_r($user, true)));
68
        $this->logger->info('User name is {name}', ['name' => $name]);
69
        $resp = new Response(200);
70
71
        $content = $this->template->render('home', [
72
            'name' => $name,
73
            'lang' => $lang,
74
            'user' => $user,
75
            'app_name' => $this->config->get('app.name'),
76
            'version' => $this->app->version(),
77
        ]);
78
79
        $resp->getBody()->write($content);
80
81
        return $resp->withHeader('Framework', 'Platine PHP');
82
    }
83
}
84