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

RequestHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 75
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 21 4
B supports() 0 20 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