Passed
Branch master (c3a259)
by Henri
01:38
created

MiddlewareTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 25
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A requestData() 0 3 3
A process() 0 7 1
A requestNamespace() 0 3 3
A requestLang() 0 3 3
1
<?php
2
3
namespace HnrAzevedo\Validator;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Server\RequestHandlerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
trait MiddlewareTrait{
10
11
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
12
    {
13
        return $handler->handle($request->withAttribute('validator', [
14
            'valid' => self::lang($this->requestLang($request))
15
                           ->namespace($this->requestNamespace($request))
16
                           ->execute($this->requestData($request)),
17
            'errors' => self::getErrors()
18
        ]));
19
    }
20
21
    private function requestData(ServerRequestInterface $request): array
22
    {
23
        return ($request->getAttribute('validator') !== null && isset($request->getAttribute('validator')['data'])) ? $request->getAttribute('validator')['data'] : $_REQUEST;
24
    }
25
26
    private function requestNamespace(ServerRequestInterface $request): array
27
    {
28
        return ($request->getAttribute('validator') !== null && isset($request->getAttribute('validator')['namespace'])) ? $request->getAttribute('validator')['namespace'] : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->getAttr...tor')['namespace'] : '' could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
29
    }
30
31
    private function requestLang(ServerRequestInterface $request): array
32
    {
33
        return ($request->getAttribute('validator') !== null && isset($request->getAttribute('validator')['lang'])) ? $request->getAttribute('validator')['lang'] : 'en';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->getAttr...idator')['lang'] : 'en' could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
34
    }
35
    
36
}