ResponseParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Lazzard/ftp-bridge package.
5
 *
6
 * (c) El Amrani Chakir <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lazzard\FtpBridge\Response;
13
14
use Lazzard\FtpBridge\Exception\ResponseParserException;
15
16
/**
17
 * Provides methods to parse a raw FTP response string.
18
 */
19
class ResponseParser
20
{
21
    /** @var string */
22
    protected $raw;
23
24
    const MATCHES = [
25
        'code'      => '/^\d+/',
26
        'message'   => '/[A-z ]+.*/',
27
        'multiline' => '/^\d{2,}-/',
28
    ];
29
30
    /**
31
     * @param string $raw
32
     */
33
    public function __construct($raw)
34
    {
35
        $this->raw = $raw;
36
    }
37
38
    /**
39
     * Parses a raw FTP response string into an array representation.
40
     *
41
     * @return array
42
     */
43
    public function parseToArray()
44
    {
45
        return array(
46
            'code'      => $this->parseCode(),
47
            'message'   => $this->parseMessage(),
48
            'multiline' => $this->isMultiline(),
49
        );
50
    }
51
52
    /**
53
     * @return false|int
54
     */
55
    protected function parseCode()
56
    {
57
        $result = preg_match(self::MATCHES['code'], $this->raw, $matches);
58
59
        if ($result === false) {
60
            throw new ResponseParserException("Failed to match " . self::MATCHES['code'] . " pattern.");
61
        }
62
63
        if (count($matches) > 0) {
64
            return (int)$matches[0];
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    protected function parseMessage()
74
    {
75
        $result = preg_match(self::MATCHES['message'], $this->raw, $matches);
76
77
        if ($result === false) {
78
            throw new ResponseParserException("Failed to match " . self::MATCHES['message'] . " pattern.");
79
        }
80
81
        return str_replace("\r",'',$matches[0]);  // remove the carriage return from the end
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    protected function isMultiline()
88
    {
89
        // according to RFC959, an FTP replay may consists of multiple lines and at least one line,
90
        // to check weather if a replay consists of multiple lines or not the RFC959 sets a convention,
91
        // for multiple lines replies the first line must be on a special format, the replay code
92
        // must immediately followed by a minus "-" character.
93
        //@link https://tools.ietf.org/html/rfc959#section-4
94
        $match = preg_match(self::MATCHES['multiline'], $this->raw);
95
96
        if ($match === false) {
97
            throw new ResponseParserException("Failed to match the response multiline pattern.");
98
        }
99
100
        return $match === 1;
101
    }
102
}