AbstractAdapter::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 9
nop 3
dl 0
loc 13
rs 9.6111
1
<?php
2
3
/**
4
 * @file AbstractAdapter.php
5
 * @brief This file contains the AbstractAdapter class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace Surfer\Adapter;
12
13
14
use Surfer\Message\Request;
15
use Surfer\Message\Response;
16
use Surfer\Hook\IChunkHook;
17
18
19
/**
20
 * @brief An abstract HTTP client adapter.
21
 * @nosubgrouping
22
 */
23
abstract class AbstractAdapter implements IClientAdapter {
24
25
  const DEFAULT_SERVER = "127.0.0.1:80"; //!< Default server.
26
  const DEFAULT_HOST = "127.0.0.1"; //!< Default host.
27
  const DEFAULT_PORT = 80; //!< Default port.
28
29
  const SCHEME_HOST_PORT_URI = '/^
30
	        (?P<scheme>tcp:\/\/|ssl:\/\/|tls:\/\/|http:\/\/|https:\/\/)? # Scheme
31
	        # Authority
32
	        (?P<host>[a-z0-9\-._~%]+                         # Named host
33
	        |     \[[a-f0-9:.]+\]                            # IPv6 host
34
	        |     \[v[a-f0-9][a-z0-9\-._~%!$&\'()*+,;=:]+\]) # IPvFuture host
35
	        (?P<port>:[0-9]+)?                               # Port
36
	        $/ix';
37
38
  // Used to know if the constructor has been already called.
39
  protected static $initialized = FALSE;
40
41
  protected $scheme;
42
  protected $host;
43
  protected $port;
44
45
  protected $userName;
46
  protected $password;
47
48
  // Stores the default ports used by the supported protocols.
49
  protected static $defaultPorts = array( // Cannot use [] syntax otherwise Doxygen generates a warning.
50
    'http://' => 80,
51
    'https://' => 443,
52
    'tcp://' => 80,
53
    'tls://' => 443,
54
    'ssl://' => 993
55
  );
56
57
  // URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100).
58
  //protected $proxy = NULL;
59
60
  // When set to TRUE, the entire URI will be used when constructing the request. While this is a non-standard request
61
  // format, some proxy servers require it.
62
  //protected $requestFullUri = FALSE;
63
64
65
  /**
66
   * @brief Creates a client adapter instance.
67
   * @param string $server Server must be expressed as host:port as defined by RFC 3986. It's also possible specify
68
   * a scheme like tcp://, ssl:// or tls://; if no scheme is present, tcp:// will be used.
69
   * @param string $userName (optional) User name.
70
   * @param string $password (optional) Password.
71
   * @see http://www.ietf.org/rfc/rfc3986.txt
72
   */
73
  public function __construct($server = self::DEFAULT_SERVER, $userName = "", $password = "") {
74
75
    // Parses the URI string '$server' to retrieve scheme, host and port and assigns matches to the relative class members.
76
    if (preg_match(self::SCHEME_HOST_PORT_URI, $server, $matches)) {
77
      $this->scheme = isset($matches['scheme']) ? $matches['scheme'] : "tcp://";
78
      $this->host = isset($matches['host']) ? $matches['host'] : self::DEFAULT_HOST;
79
      $this->port = isset($matches['port']) ? (int) substr($matches['port'], 1) : self::$defaultPorts[$this->scheme];
80
    }
81
    else // Match attempt failed.
82
      throw new \InvalidArgumentException(sprintf("'%s' is not a valid URI.", $server));
83
84
    $this->userName = (string)$userName;
85
    $this->password = (string)$password;
86
  }
87
88
89
  /**
90
   * @brief Initializes the client.
91
   * @details This method is called just once, when the first object instance is created. It's used to execute one time
92
   * operations due to initialize the client. Even if you create many instance of this client, this method is executed
93
   * just once, keep it in mind.
94
   */
95
  abstract public function initialize();
96
97
98
99
  /**
100
   * @brief Sets the maximum number of seconds the request will take to be performed.
101
   * @param int $seconds The timeout in seconds.
102
   * @return bool Returns `true` on success or `false` on failure.
103
   */
104
  abstract public function setTimeout($seconds);
105
106
107
  /**
108
   * @brief This method is used to send an HTTP Request.
109
   * @details You can also provide an instance of a class that implements the IChunkHook interface, to deal with a chunked
110
   * response.
111
   * @param Request $request The Request object.
112
   * @param IChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface.
113
   * @return Response
114
   */
115
  abstract public function send(Request $request, IChunkHook $chunkHook = NULL);
116
117
}