AbstractMapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 51
ccs 12
cts 13
cp 0.9231
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processAliasing() 0 12 3
A __construct() 0 4 1
A supports() 0 3 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
8
namespace Zicht\Bundle\UrlBundle\Aliasing\Mapper;
9
10
use Zicht\Bundle\UrlBundle\Url\Rewriter;
11
12
/**
13
 * Base class for simple match mapping
14
 */
15
abstract class AbstractMapper implements UrlMapperInterface
16
{
17
    /**
18
     * @var array|\string[]
19
     */
20
    protected $contentTypes;
21
22
    /**
23
     * @var string
24
     */
25
    protected $pattern;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param string[] $contentTypes
31
     * @param string $pattern
32
     */
33 8
    public function __construct(array $contentTypes, $pattern)
34
    {
35 8
        $this->contentTypes = $contentTypes;
36 8
        $this->pattern = $pattern;
37 8
    }
38
39
    /**
40
     * Check if the mapper supports the given contentType
41
     *
42
     * @param string $contentType
43
     *
44
     * @return boolean
45
     */
46 3
    public function supports($contentType)
47
    {
48 3
        return in_array($contentType, $this->contentTypes);
49
    }
50
51
    /**
52
     * @{inheritDoc}
53
     */
54 5
    public function processAliasing($content, $mode, Rewriter $rewriter)
55
    {
56 5
        if (!preg_match_all($this->pattern, $content, $matches, PREG_SET_ORDER)) {
57
            // early return: if there are no matches, no need for the rest of the processing.
58
            return $content;
59
        }
60 5
        $groups = [];
61 5
        foreach ($matches as $match) {
62 5
            $groups[$match[2]][]= $match;
63
        }
64
65 5
        return $rewriter->rewriteMatches($content, $mode, $groups);
66
    }
67
}
68