MailtoBuilder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getScheme() 0 3 1
A raw() 0 3 1
A getPathBag() 0 3 1
A build() 0 13 2
A getQueryBag() 0 3 1
A all() 0 6 1
A populate() 0 8 1
A __construct() 0 7 2
A encoded() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Builder\Schemes\Mailto;
5
6
use Keppler\Url\Builder\Schemes\Mailto\Bags\MailtoPathMutable;
7
use Keppler\Url\Builder\Schemes\Mailto\Bags\MailtoQueryMutable;
8
use Keppler\Url\Exceptions\InvalidComponentsException;
9
use Keppler\Url\Interfaces\Mutable\MutableSchemeInterface;
10
use Keppler\Url\Scheme\Schemes\Mailto\MailtoImmutable;
11
use Keppler\Url\Traits\Accessor;
12
use Keppler\Url\Traits\Mutator;
13
14
/**
15
 * Class MailtoBuilder
16
 *
17
 * @package Keppler\Url\Builder\Schemes\MailtoImmutable
18
 */
19
class MailtoBuilder implements MutableSchemeInterface
20
{
21
    use Mutator;
22
    use Accessor;
23
24
    /**
25
     * The default scheme for this class
26
     *
27
     * @var string
28
     */
29
    const SCHEME = 'mailto';
30
31
    /**
32
     * @var MailtoQueryMutable
33
     */
34
    private $queryBag;
35
36
    /**
37
     * @var MailtoPathMutable
38
     */
39
    private $pathBag;
40
41
    /**
42
     * MailtoBuilder constructor.
43
     *
44
     * @param MailtoImmutable|null $mailto
45
     * @throws InvalidComponentsException
46
     */
47
    public function __construct(MailtoImmutable $mailto = null)
48
    {
49
        $this->queryBag = new MailtoQueryMutable();
50
        $this->pathBag = new MailtoPathMutable();
51
52
        if (null !== $mailto) {
53
            $this->populate($mailto);
54
        }
55
    }
56
57
    /**
58
     * @param MailtoImmutable $mailto
59
     * @throws InvalidComponentsException
60
     */
61
    private function populate(MailtoImmutable $mailto): void
62
    {
63
        $this->queryBag->setCc($mailto->getQueryBag()->getCc());
64
        $this->queryBag->setBcc($mailto->getQueryBag()->getBcc());
65
        $this->queryBag->setTo($mailto->getQueryBag()->getTo());
66
        $this->queryBag->setSubject($mailto->getQueryBag()->getSubject());
67
        $this->queryBag->setBody($mailto->getQueryBag()->getBody());
68
        $this->pathBag->setPath($mailto->getPathBag()->getPath());
69
    }
70
71
/////////////////////////
72
/// GETTER FUNCTIONS  ///
73
////////////////////////
74
75
    /**
76
     * @return MailtoQueryMutable|null
77
     */
78
    public function getQueryBag()
79
    {
80
        return $this->queryBag;
81
    }
82
83
    /**
84
     * @return MailtoPathMutable
85
     */
86
    public function getPathBag()
87
    {
88
        return $this->pathBag;
89
    }
90
91
/////////////////////////////////
92
/// INTERFACE IMPLEMENTATION  ///
93
/////////////////////////////////
94
95
    /**
96
     * @param bool $urlEncode
97
     *
98
     * @return string
99
     */
100
    public function build(bool $urlEncode = false): string
101
    {
102
        $url = self::SCHEME.':';
103
104
        if ($urlEncode) {
105
            $url .= $this->pathBag->encoded();
106
            $url .= $this->queryBag->encoded();
107
        } else {
108
            $url .= $this->pathBag->raw();
109
            $url .= $this->queryBag->raw();
110
        }
111
112
        return $url;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function all(): array
119
    {
120
        return [
121
            'scheme' => self::SCHEME,
122
            'path' => $this->pathBag->all(),
123
            'query' => $this->queryBag->all(),
124
        ];
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function raw(): string
131
    {
132
        return $this->build(false);
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function encoded(): string
139
    {
140
        return $this->build(true);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getScheme(): string
147
    {
148
        return self::SCHEME;
149
    }
150
}
151