LanguageAwareAliasingStrategy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 57
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocalesToPrefix() 0 3 1
A __construct() 0 5 1
A generatePublicAlias() 0 11 4
1
<?php
2
/**
3
 * @author Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\PageBundle\Aliasing\Strategy;
8
9
use Zicht\Bundle\UrlBundle\Aliasing\AliasingStrategy;
10
11
/**
12
 * Class LanguageAwareAliasingStrategy
13
 *
14
 * @package Zicht\Bundle\UrlBundle\Url\Aliasing\Strategy
15
 */
16
class LanguageAwareAliasingStrategy implements AliasingStrategy
17
{
18
    /**
19
     * @var string
20
     */
21
    public $basePath;
22
23
    /**
24
     * @var array
25
     */
26
    protected $localesToPrefix;
27
28
    /**
29
     * @var AliasingStrategy
30
     */
31
    protected $strategyWrapper;
32
33
    /**
34
     * LanguageAwareAliasingStrategy constructor.
35
     *
36
     * @param AliasingStrategy $strategyWrapper
37
     * @param array $localesToPrefix
38
     */
39
    public function __construct(AliasingStrategy $strategyWrapper, $localesToPrefix = array())
40
    {
41
        $this->basePath = '/';
42
        $this->strategyWrapper = $strategyWrapper;
43
        $this->localesToPrefix = $localesToPrefix;
44
    }
45
46
    /**
47
     * Generate public alias
48
     *
49
     * @param mixed $subject
50
     * @param string $currentAlias
51
     * @return string
52
     */
53
    public function generatePublicAlias($subject, $currentAlias = '')
54
    {
55
        $alias = $this->strategyWrapper->generatePublicAlias($subject, $currentAlias);
56
57
        if ($alias !== null && method_exists($subject, 'getLanguage')) {
58
            if (in_array($subject->getLanguage(), $this->localesToPrefix)) {
59
                $alias = sprintf('%s%s%s', $this->basePath, $subject->getLanguage(), $alias);
60
            }
61
        }
62
63
        return $alias;
64
    }
65
66
    /**
67
     * @param array $localesToPrefix
68
     * @return void
69
     */
70
    public function setLocalesToPrefix($localesToPrefix)
71
    {
72
        $this->localesToPrefix = $localesToPrefix;
73
    }
74
}
75