DefaultAliasingStrategy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
eloc 15
c 4
b 0
f 3
dl 0
loc 52
ccs 17
cts 17
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A toAlias() 0 3 1
A generatePublicAlias() 0 19 6
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Aliasing;
7
 
8
use Zicht\Util\Str;
9
10
/**
11
 * An aliasing strategy which simply puts the systemized title at the specified base path (default is root)
12
 */
13
class DefaultAliasingStrategy implements AliasingStrategy
14
{
15
    /**
16
     * Construct with the specified base path
17
     *
18
     * @param string $basePath
19
     */
20 6
    public function __construct($basePath = '/')
21
    {
22 6
        $this->basePath = $basePath;
0 ignored issues
show
Bug Best Practice introduced by
The property basePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23 6
    }
24
25
26
    /**
27
     * Returns the calculated public alias for the specified object.
28
     *
29
     * @param string $subject
30
     * @param string $currentAlias
31
     * @return null|string
32
     * @throws \InvalidArgumentException
33
     */
34 6
    public function generatePublicAlias($subject, $currentAlias = '')
35
    {
36 6
        if (is_object($subject)) {
0 ignored issues
show
introduced by
The condition is_object($subject) is always false.
Loading history...
37 4
            if ($subject instanceof Aliasable) {
38 1
                $subject = (string)$subject->getAliasTitle();
39 3
            } elseif (method_exists($subject, 'getTitle')) {
40 1
                $subject = (string)$subject->getTitle();
41
            } else {
42 2
                $subject = (string)$subject;
43
            }
44
        }
45 6
        if (!is_string($subject)) {
0 ignored issues
show
introduced by
The condition is_string($subject) is always true.
Loading history...
46 1
            throw new \InvalidArgumentException("Expected a string or object as subject, got " . gettype($subject));
47
        }
48
49 5
        if ($alias = $this->toAlias($subject)) {
50 4
            return $this->basePath . $alias;
51
        }
52 1
        return null;
53
    }
54
55
56
    /**
57
     * Systemizes the specified string.
58
     *
59
     * @param string $title
60
     * @return string
61
     */
62 5
    protected function toAlias($title)
63
    {
64 5
        return Str::systemize($title);
65
    }
66
}
67