DefaultAliasingStrategy::generatePublicAlias()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 6
eloc 12
c 3
b 0
f 3
nc 12
nop 2
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
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