BasePathTrait::basePath()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
/**
6
 * Utilities used by middlewares with basePath options.
7
 */
8
trait BasePathTrait
9
{
10
    private $basePath = '';
11
12
    /**
13
     * Set the basepath used in the request.
14
     *
15
     * @param string $basePath
16
     *
17
     * @return self
18
     */
19
    public function basePath($basePath)
20
    {
21
        if (strlen($basePath) > 1 && substr($basePath, -1) === '/') {
22
            $this->basePath = substr($basePath, 0, -1);
23
        } else {
24
            $this->basePath = $basePath;
25
        }
26
27
        return $this;
28
    }
29
30
    /**
31
     * Removes the basepath from a path.
32
     *
33
     * @param string $path
34
     *
35
     * @return string
36
     */
37
    private function getPath($path)
38
    {
39
        if ($this->basePath === '' || strpos($path, $this->basePath) === 0) {
40
            $path = substr($path, strlen($this->basePath)) ?: '';
41
        }
42
43
        return $path === '' ? '/' : $path;
44
    }
45
}
46