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

RequestHandler::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2
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
     * @return AdminInterface
31
     * @throws Exception
32
     */
33 1
    public function handle(Request $request)
34
    {
35 1
        $routeParameters = $request->get('_route_params');
36
37 1
        if (!is_array($routeParameters)) {
38 1
            throw new Exception(
39 1
                'Cannot find admin from request. _route_params parameters for request not found or invalid'
40
            );
41
        }
42 1
        if (!array_key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters)) {
43 1
            throw new Exception('Cannot find admin from request. "_admin" route parameter is missing');
44
        }
45 1
        if (!array_key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)) {
46 1
            throw new Exception('Cannot find admin action from request. "_action" route parameter is missing');
47
        }
48
49
        return $this
50 1
            ->registry
51 1
            ->get($routeParameters['_admin']);
52
    }
53
    
54
    /**
55
     * Return true if the current Request is supported. Supported means that the Request has the required valid
56
     * parameters to get an admin from the registry.
57
     *
58
     * @param Request $request
59
     *
60
     * @return boolean
61
     */
62 1
    public function supports(Request $request)
63
    {
64 1
        $routeParameters = $request->get('_route_params');
65
    
66 1
        if (!is_array($routeParameters)) {
67 1
            return false;
68
        }
69
    
70 1
        if (!array_key_exists(LAGAdminBundle::REQUEST_PARAMETER_ADMIN, $routeParameters) ||
71 1
            !array_key_exists(LAGAdminBundle::REQUEST_PARAMETER_ACTION, $routeParameters)
72
        ) {
73 1
            return false;
74
        }
75
    
76 1
        if (!$this->registry->has($routeParameters[LAGAdminBundle::REQUEST_PARAMETER_ADMIN])) {
77 1
            return false;
78
        }
79
        
80 1
        return true;
81
    }
82
}
83