PrefixModifier::getPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
  namespace ReRoute\Modifier;
4
5
  /**
6
   * @package ReRoute\Modifier
7
   */
8
  class PrefixModifier extends AbstractRouteModifier {
9
10
11
    /**
12
     * @var string
13
     */
14
    public $prefix;
15
16
17
    /**
18
     * @param string $prefix
19
     *
20
     * @return PrefixModifier
21
     */
22 6
    public function setPrefix($prefix) {
23 6
      $this->prefix = $prefix;
24 6
      return $this;
25
    }
26
27
28
    /**
29
     * @return string
30
     */
31 6
    public function getPrefix() {
32 6
      return $this->prefix;
33
    }
34
35
36
    /**
37
     * @inheritdoc
38
     */
39 6
    public function isMatched(\ReRoute\RequestContext $requestContext) {
40 6
      $prefix = $this->getPrefix();
41 6
      if (empty($prefix)) {
42
        return true;
43
      }
44 6
      if (strpos($requestContext->getPath(), $prefix) === 0) {
45 6
        $pathWithoutPrefix = (string) substr($requestContext->getPath(), strlen($prefix));
46 6
        if (substr($pathWithoutPrefix, 0, 1) != '/') {
47 6
          $pathWithoutPrefix = '/' . $pathWithoutPrefix;
48 6
        }
49 6
        $requestContext->setPath($pathWithoutPrefix);
50 6
        return true;
51
      }
52 3
      return false;
53
    }
54
55
56
    /**
57
     * @inheritdoc
58
     */
59 3
    public function build(\ReRoute\Url $url, \ReRoute\UrlBuilder $urlBuilder) {
60 3
      $prefix = $this->getPrefix();
61 3
      if (!empty($prefix)) {
62 3
        $url->setPath($this->getPrefix() . $url->getPath());
63 3
      }
64
    }
65
  }