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

RequestHandler::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 4
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
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
     * @throws Exception
34
     */
35 1
    public function handle(Request $request)
36
    {
37 1
        $routeParameters = $request->get('_route_params');
38
39 1
        if (!is_array($routeParameters)) {
40 1
            throw new Exception(
41 1
                'Cannot find admin from request. _route_params parameters for request not found or invalid'
42
            );
43
        }
44 1
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters)) {
45 1
            throw new Exception('Cannot find admin from request. "_admin" route parameter is missing');
46
        }
47 1
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)) {
48 1
            throw new Exception('Cannot find admin action from request. "_action" route parameter is missing');
49
        }
50
51
        return $this
52 1
            ->registry
53 1
            ->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
     *
63
     * @return boolean
64
     */
65 1
    public function supports(Request $request)
66
    {
67 1
        $routeParameters = $request->get('_route_params');
68
    
69 1
        if (!is_array($routeParameters)) {
70 1
            return false;
71
        }
72
    
73 1
        if (!key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters) ||
74 1
            !key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)
75
        ) {
76 1
            return false;
77
        }
78
    
79 1
        if (!$this->registry->has($routeParameters[LAGAdminBundle::REQUEST_PARAMETER_ADMIN])) {
80 1
            return false;
81
        }
82
        
83 1
        return true;
84
    }
85
}
86