RequestMatcher::match()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0585

Importance

Changes 5
Bugs 2 Features 4
Metric Value
c 5
b 2
f 4
dl 0
loc 25
ccs 15
cts 17
cp 0.8824
rs 8.439
cc 6
eloc 14
nc 10
nop 1
crap 6.0585
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
}