Requests_Auth_Basic   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A register() 0 4 1
A curl_before_send() 0 4 1
A fsockopen_header() 0 3 1
A getAuthString() 0 3 1
1
<?php
2
/**
3
 * Basic Authentication provider
4
 *
5
 * @package Requests
6
 * @subpackage Authentication
7
 */
8
9
/**
10
 * Basic Authentication provider
11
 *
12
 * Provides a handler for Basic HTTP authentication via the Authorization
13
 * header.
14
 *
15
 * @package Requests
16
 * @subpackage Authentication
17
 */
18
class Requests_Auth_Basic implements Requests_Auth {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
19
	/**
20
	 * Username
21
	 *
22
	 * @var string
23
	 */
24
	public $user;
25
26
	/**
27
	 * Password
28
	 *
29
	 * @var string
30
	 */
31
	public $pass;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`)
37
	 * @param array|null $args Array of user and password. Must have exactly two elements
38
	 */
39
	public function __construct($args = null) {
40
		if (is_array($args)) {
41
			if (count($args) !== 2) {
42
				throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs');
43
			}
44
45
			list($this->user, $this->pass) = $args;
46
		}
47
	}
48
49
	/**
50
	 * Register the necessary callbacks
51
	 *
52
	 * @see curl_before_send
53
	 * @see fsockopen_header
54
	 * @param Requests_Hooks $hooks Hook system
55
	 */
56
	public function register(Requests_Hooks &$hooks) {
57
		$hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
58
		$hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
59
	}
60
61
	/**
62
	 * Set cURL parameters before the data is sent
63
	 *
64
	 * @param resource $handle cURL resource
65
	 */
66
	public function curl_before_send(&$handle) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
67
		curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
68
		curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString());
69
	}
70
71
	/**
72
	 * Add extra headers to the request before sending
73
	 *
74
	 * @param string $out HTTP header string
75
	 */
76
	public function fsockopen_header(&$out) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
77
		$out .= sprintf("Authorization: Basic %s\r\n", base64_encode($this->getAuthString()));
78
	}
79
80
	/**
81
	 * Get the authentication string (user:pass)
82
	 *
83
	 * @return string
84
	 */
85
	public function getAuthString() {
86
		return $this->user . ':' . $this->pass;
87
	}
88
}