Connection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 68.28%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 10
c 4
b 0
f 1
lcom 3
cbo 0
dl 0
loc 129
ccs 28
cts 41
cp 0.6828
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setOptions() 0 4 1
A connection() 0 14 2
A result() 0 8 1
A options() 0 9 1
A init() 0 17 3
1
<?php
2
3
namespace Chay22\RecSelMeter;
4
5
abstract class Connection
6
{
7
	/**
8
	 * URL to execute
9
	 * 
10
	 * @var $url
11
	 */
12
	public $url;
13
14
	/**
15
	 * Contains curl_init() if success
16
	 *
17
	 * @var $init
18
	 */
19
	protected $init = false;
20
21
	/**
22
	 *  Additional cURL options
23
	 *  
24
	 *  @var $options
25
	 */
26
	public $options = [];
27
28
	/**
29
	 * @var $header
30
	 */
31
	public $header = [];
32
33
	/**
34
	 * @var $content
35
	 */
36
	public $content;
37
38
	/**
39
	 * @param  string  $url  Store URL
40
	 */
41 1
	function __construct($url)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
	{	
43 1
		if (!function_exists('curl_version')) {
44
			throw new \Exception('This library needs cURL to run!');
45
		}
46 1
		$this->init = $this->init($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->init($url) of type resource is incompatible with the declared type boolean of property $init.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47 1
		$this->connection();
48 1
	}
49
50
	/**
51
	 * Set additional curl_setopt_array() options
52
	 * 
53
	 * @param 	array 	$options 	cURL Options
54
	 * @see http://php.net/manual/en/function.curl-setopt.php
55
	 * @return array
56
	 */
57
	public function setOptions($options = [])
58
	{
59
		return $this->options = $options;
60
	}
61
62
	/**
63
	 * Running the curl session
64
	 * 
65
	 * @return void
66
	 */
67 1
	public function connection()
68
	{
69
		
70 1
		$ch = $this->init;
71 1
        	curl_setopt_array($ch, ($this->options() + $this->options));
72 1
        	if (!$this->content = curl_exec($ch)){
73
        		throw new \Exception(
74
        			'CURL ERROR! Code: ' . curl_errno($ch) .
75
        		'	Message: ' . curl_error($ch)
76
        		);
77
        	}
78 1
        	$this->header = curl_getinfo($ch);
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_getinfo($ch) of type * is incompatible with the declared type array of property $header.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79 1
        	curl_close($ch);
80 1
	}
81
82
	/**
83
	 * Get result of both header and content from curl
84
	 * 
85
	 * @return array
86
	 */
87
	public function result()
88
	{
89
		return [
90
			'headers', $this->connection['headers'],
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
91
			'header', $this->connection['header'],
92
			'content', $this->connection['content'],
93
		       ];
94
	}
95
96
	/**
97
	 * Set cURL options
98
	 * 
99
	 * @return array
100
	 */
101 1
	protected function options()
102
	{
103
		return [
104 1
			CURLOPT_RETURNTRANSFER => true,
105 1
			CURLOPT_FOLLOWLOCATION => true,
106 1
            		CURLOPT_ENCODING       => '',
107 1
            		CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
108 1
		       ];
109
	}
110
111
	/**
112
	 * Validate URL and start cURL session;
113
	 *
114
	 * @param  string  $url  Store URL
115
	 */
116 1
	protected function init($url)
117
	{
118
		//Grab url id in case $url is not valid as required url
119 1
		$id = $url;
120 1
		if (strpos($url, 'product') !== false) {
121 1
    			$id = explode('product',$url);
122 1
    			$id = end($id);
123 1
    			$id = explode('/', $id)[1];
124 1
		}
125
		
126
		//Set the $id as required url
127 1
		$this->url = 'http://fjb.kaskus.co.id/product/' . $id;
128 1
		if (!curl_init($url)) {
129
			throw new \Exception('Can\'t start cURL session.');
130
		}
131 1
		return curl_init($url);
132
	}
133
}
134