RequestMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 72.41%

Importance

Changes 6
Bugs 2 Features 5
Metric Value
wmc 11
c 6
b 2
f 5
cbo 1
dl 0
loc 51
ccs 21
cts 29
cp 0.7241
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B match() 0 25 6
A reverse() 0 9 3
1
<?php
2
namespace Fwk\Core\Components\RequestMatcher;
3
4
use Symfony\Component\HttpFoundation\Request;
5
6
class RequestMatcher
7
{
8
    const DEFAULT_ACTION_REGEX  =   '/^([A-Z0-9a-z_][^\.]+)\.action/';
9
    
10
    protected $actionRegex;
11
    
12 27
    public function __construct($actionRegex = null)
13
    {
14 27
        if (null === $actionRegex) {
15 27
            $actionRegex = self::DEFAULT_ACTION_REGEX;
16 27
        }
17
        
18 27
        $this->actionRegex = $actionRegex;
19 27
    }
20
    
21 26
    public function match(Request $request)
22
    {
23 26
        $baseUri     = $request->getBaseUrl();
24 26
        $uri         = $request->getRequestUri();
25
26 26
        if(!empty($baseUri) && \strpos($uri, $baseUri) === 0) {
27
            $uri    = \substr($uri, strlen($baseUri));
28
        }
29
30 26
        $uri         = trim($uri, '/');
31 26
        if (empty($uri)) {
32 2
            return null;
33
        }
34
        
35 24
        if (strpos($uri, '?') > 0) {
36 7
            list($uri,) = explode('?', $uri);
37 7
        }
38
        
39 24
        $actionName  = false;
40 24
        if (\preg_match($this->actionRegex, $uri, $matches)) {
41 20
            $actionName = $matches[1];
42 20
        }
43
        
44 24
        return $actionName;
45
    }
46
    
47
    public function reverse($actionName, array $params = array(), 
48
        $escapeAmp = false
49
    ) {
50
        return sprintf(
51
            '/%s.action%s', 
52
            $actionName, 
53
            (!count($params) ? null : '?' . http_build_query($params, '', ($escapeAmp === true ? '&amp;' : '&')))
54
        );
55
    }
56
}