Passed
Push — master ( 20361a...82a2cb )
by Tobias
01:15
created

Path::withPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Dsn\Configuration;
6
7
/**
8
 * A "path like" DSN string.
9
 *
10
 * Example:
11
 * - redis:///var/run/redis/redis.sock
12
 * - memcached://user:password@/var/local/run/memcached.socket?weight=25
13
 *
14
 * @author Tobias Nyholm <[email protected]>
15
 */
16
class Path extends Dsn
17
{
18
    use UserPasswordTrait;
19
    /**
20
     * @var string
21
     */
22
    private $path;
23
24
    public function __construct(string $scheme, string $path, array $parameters = [], array $authentication = [])
25
    {
26
        $this->path = $path;
27
        $this->setAuthentication($authentication);
28
        parent::__construct($scheme, $parameters);
29
    }
30
31
    public function getScheme(): string
32
    {
33
        return parent::getScheme();
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getScheme() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
34
    }
35
36
    public function getPath(): string
37
    {
38
        return $this->path;
39
    }
40
41
    public function withPath(string $path): self
42
    {
43
        $new = clone $this;
44
        $new->path = $path;
45
46
        return $new;
47
    }
48
49
    /**
50
     * @var string
51
     */
52
    public function __toString()
53
    {
54
        $parameters = $this->getParameters();
55
56
        return
57
            $this->getScheme().'://'.
58
            $this->getUserInfoString().
59
            $this->getPath().
60
            (empty($parameters) ? '' : '?'.http_build_query($parameters));
61
    }
62
}
63