FtpImmutable::__construct()   B
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 14
nc 32
nop 1
dl 0
loc 26
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Scheme\Schemes\Ftp;
5
6
use Keppler\Url\Interfaces\Immutable\ImmutableSchemeInterface;
7
use Keppler\Url\Scheme\Schemes\AbstractImmutable;
8
use Keppler\Url\Scheme\Schemes\Ftp\Bags\FtpImmutablePath;
9
10
/**
11
 * Note that the following class makes no assumption regarding url encoding
12
 * the ftp url is taken AS IS and will not be decoded or encoded
13
 * url encoded strings WILL result in errors
14
 *
15
 * ftpurl = "ftp://" login [ "/" fpath [ ";type=" ftptype ]]
16
 *
17
 * ftp://[user[:password]@]host[:port]/url-path
18
 *
19
 * @see https://tools.ietf.org/html/rfc1738
20
 *
21
 * Class FtpImmutable
22
 *
23
 * @package Keppler\Url\Schemes\Ftp
24
 */
25
class FtpImmutable extends AbstractImmutable implements ImmutableSchemeInterface
26
{
27
    /**
28
     * The default scheme for this class
29
     *
30
     * @var string
31
     */
32
    const SCHEME = 'ftp';
33
34
    /**
35
     * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
36
     *
37
     * @var string
38
     */
39
    private $user = '';
40
41
    /**
42
     * Usage is highly discouraged
43
     *
44
     * @var string
45
     */
46
    private $password = '';
47
48
    /**
49
     * host = IP-literal / IPv4address / reg-name
50
     *
51
     * @var string
52
     */
53
    private $host = '';
54
55
    /**
56
     * port = *DIGIT
57
     *
58
     * Ports can't be negative, -1 should be considered the default value and ignored
59
     *
60
     * INVALID-PORT
61
     *  Either the local or foreign port was improperly
62
     *  specified.  This should be returned if either or
63
     *  both of the port ids were out of range (TCP port
64
     *  numbers are from 1-65535), negative integers, reals or
65
     *  in any fashion not recognized as a non-negative integer.
66
     *
67
     * @see https://www.ietf.org/rfc/rfc1413.txt
68
     * @var int
69
     */
70
    private $port = -1;
71
72
    /**
73
     * @var FtpImmutablePath
74
     */
75
    private $pathBag;
76
77
    /**
78
     * @var string
79
     */
80
    private $raw;
81
82
    /**
83
     * MailtoImmutable constructor.
84
     * @param $url
85
     */
86
    public function __construct(string $url)
87
    {
88
        $this->raw = $url;
89
90
        $parsedUrl = parse_url($url);
91
92
        if (isset($parsedUrl['path']) && !empty($parsedUrl['path'])) {
93
            $this->pathBag = new FtpImmutablePath($parsedUrl['path']);
94
        } else {
95
            $this->pathBag = new FtpImmutablePath();
96
        }
97
98
        if (isset($parsedUrl['user']) && !empty($parsedUrl['user'])) {
99
            $this->user = $parsedUrl['user'];
100
        }
101
102
        if (isset($parsedUrl['host']) && !empty($parsedUrl['host'])) {
103
            $this->host = $parsedUrl['host'];
104
        }
105
106
        if (isset($parsedUrl['port']) && !empty($parsedUrl['port'])) {
107
            $this->port = $parsedUrl['port'];
108
        }
109
110
        if (isset($parsedUrl['pass']) && !empty($parsedUrl['pass'])) {
111
            $this->password = $parsedUrl['pass'];
112
        }
113
    }
114
115
//////////////////////////
116
/// GETTER FUNCTIONS  ///
117
////////////////////////
118
119
    /**
120
     * @return FtpImmutablePath
121
     */
122
    public function getPathBag(): FtpImmutablePath
123
    {
124
        return $this->pathBag;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getUser(): string
131
    {
132
        return $this->user;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getPassword(): string
139
    {
140
        return $this->password;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getHost(): string
147
    {
148
        return $this->host;
149
    }
150
151
    /**
152
     * @return int
153
     */
154
    public function getPort(): ?int
155
    {
156
        return -1 === $this->port ? null : $this->port;
157
    }
158
159
/////////////////////////////////
160
/// INTERFACE IMPLEMENTATIOM  ///
161
////////////////////////////////
162
163
    /**
164
     * Returns all the components of the scheme including
165
     *  any bags in the form of an array
166
     *
167
     * @return array
168
     */
169
    public function all(): array
170
    {
171
        return [
172
            'scheme' => self::SCHEME,
173
            'user' => $this->user,
174
            'password' => $this->password,
175
            'host' => $this->host,
176
            'port' => -1 === $this->port ? '' : $this->port,
177
            'path' => $this->pathBag->all(),
178
        ];
179
    }
180
181
    /**
182
     * Return the raw unaltered url
183
     *
184
     * @return string
185
     */
186
    public function raw(): string
187
    {
188
        return $this->raw;
189
    }
190
191
    /**
192
     * Returns the scheme associated with the class
193
     *
194
     * @return string
195
     */
196
    public function getScheme(): string
197
    {
198
        return self::SCHEME;
199
    }
200
}
201