Completed
Pull Request — master (#90)
by Arnaud
02:11
created

RequestHandler::supports()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 5
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Request;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use LAG\AdminBundle\Admin\Registry\Registry;
8
use LAG\AdminBundle\LAGAdminBundle;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class RequestHandler implements RequestHandlerInterface
12
{
13
    /**
14
     * @var Registry
15
     */
16
    protected $registry;
17
    
18
    /**
19
     * RequestHandler constructor.
20
     *
21
     * @param Registry $registry
22
     */
23 2
    public function __construct(Registry $registry)
24
    {
25 2
        $this->registry = $registry;
26 2
    }
27
    
28
    /**
29
     * @param Request $request
30
     *
31
     * @return AdminInterface
32
     *
33 1
     * @throws Exception
34
     */
35 1
    public function handle(Request $request)
36
    {
37 1
        $routeParameters = $request->get('_route_params');
38 1
39 1
        if (!is_array($routeParameters)) {
40
            throw new Exception(
41
                'Cannot find admin from request. _route_params parameters for request not found or invalid'
42 1
            );
43 1
        }
44
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters)) {
45 1
            throw new Exception('Cannot find admin from request. "_admin" route parameter is missing');
46 1
        }
47
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)) {
48
            throw new Exception('Cannot find admin action from request. "_action" route parameter is missing');
49
        }
50 1
51 1
        return $this
52
            ->registry
53
            ->get($routeParameters['_admin'])
54
        ;
55
    }
56
    
57
    /**
58
     * Return true if the current Request is supported. Supported means that the Request has the required valid
59
     * parameters to get an admin from the registry.
60
     *
61
     * @param Request $request
62 1
     *
63
     * @return boolean
64 1
     */
65
    public function supports(Request $request)
66 1
    {
67 1
        $routeParameters = $request->get('_route_params');
68
    
69
        if (!is_array($routeParameters)) {
70 1
            return false;
71 1
        }
72
    
73 1
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters) ||
74
            !key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)
75
        ) {
76 1
            return false;
77 1
        }
78
    
79
        if (!$this->registry->has($routeParameters[LAGAdminBundle::REQUEST_PARAMETER_ADMIN])) {
80 1
            return false;
81
        }
82
        
83
        return true;
84
    }
85
}
86