Passed
Push — features/scrutinizer_conf ( cf2d4a...45dfb6 )
by Nicolas
01:09
created

LiveboxTools::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 3
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nicolas
5
 * Date: 05/01/18
6
 * Time: 12:00
7
 */
8
9
namespace Devgiants\Service;
10
11
use Buzz\Browser;
0 ignored issues
show
Bug introduced by
The type Buzz\Browser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Buzz\Message\Request;
0 ignored issues
show
Bug introduced by
The type Buzz\Message\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Buzz\Util\Cookie;
0 ignored issues
show
Bug introduced by
The type Buzz\Util\Cookie was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Buzz\Util\CookieJar;
0 ignored issues
show
Bug introduced by
The type Buzz\Util\CookieJar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Buzz\Message\MessageInterface;
0 ignored issues
show
Bug introduced by
The type Buzz\Message\MessageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Buzz\Util\Url;
0 ignored issues
show
Bug introduced by
The type Buzz\Util\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class LiveboxTools {
20
21
	/**
22
	 * @var Browser
23
	 */
24
	protected $browser;
25
26
	/**
27
	 * @var string
28
	 */
29
	protected $currentToken;
30
31
	/**
32
	 * @var CookieJar
33
	 */
34
	protected $cookieJar;
35
36
	/**
37
	 * LiveboxTools constructor.
38
	 */
39
	public function __construct() {
40
		$this->browser = new Browser();
41
		$this->cookieJar = new CookieJar();
42
	}
43
44
	/**
45
	 * @return Browser
46
	 */
47
	public function getBrowser() {
48
		return $this->browser;
49
	}
50
51
52
	/**
53
	 * @param $host
54
	 * @param $username
55
	 * @param $password
56
	 *
57
	 * @return mixed
58
	 */
59
	public function authenticate( $host, $username, $password ) {
60
61
		// Get token response
62
		$response = $this->browser->post(
63
			"$host/ws",
64
			[
65
				'Content-Type'  => 'application/x-sah-ws-1-call+json; charset=UTF-8',
66
				'Authorization' => 'X-Sah-Login',
67
			],
68
			json_encode( [
69
				'service'    => 'sah.Device.Information',
70
				'method'     => 'createContext',
71
				'parameters' => [
72
					'applicationName' => 'so_sdkut',
73
					'username'        => $username,
74
					'password'        => $password,
75
				],
76
			] )
77
78
		);
79
80
81
		// Create sessid cookie
82
		$cookie = new Cookie();
83
		$cookie->fromSetCookieHeader( $response->getHeader( 'Set-Cookie' ), $host );
84
85
		// Add cookie to JAR
86
		$this->cookieJar->addCookie( $cookie );
87
88
		//		foreach($response->getHeader('Set-Cookie') as $cookieString) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
		//			$cookie = new CookieJar();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
		//		}
91
92
		//		$response = $this->client->post( "$host/ws", [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
		//			RequestOptions::HEADERS => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
		//				'Content-Type'  => 'application/x-sah-ws-1-call+json; charset=UTF-8',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
		//				'Authorization' => 'X-Sah-Login',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
		//			],
97
		//			RequestOptions::JSON    => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
		//				'service'    => 'sah.Device.Information',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
		//				'method'     => 'createContext',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
		//				'parameters' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
		//					'applicationName' => 'so_sdkut',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
		//					'username'        => $username,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
		//					'password'        => $password,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
		//				],
105
		//			],
106
		//		] );
107
108
109
		//		foreach ( $response->getHeader( 'Set-Cookie' ) as $cookieString ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
		//			$cookie = SetCookie::fromString( $cookieString );
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
		//			$cookie->setDomain( '192.168.1.1' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
		//			//			var_dump( $this->cookieJar->setCookie($cookie));
113
		//			$this->cookieJar->save();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
		//		}
115
		//		echo($this->cookieJar->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
117
		//		var_dump( $this->cookieJar->toArray() );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
		//		die();
119
120
		$json = json_decode( $response->getContent() );
121
122
		if ( ( $json->status === 0 ) && isset( $json->data->contextID ) ) {
123
			$this->currentToken = $json->data->contextID;
124
125
			return $json->data->contextID;
126
		} else {
127
			// TODO handle
128
			throw new \AuthenticationException();
129
		}
130
	}
131
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getCookieHeaderForRequest() {
137
		$cookieString = "Cookie: ";
138
139
		foreach($this->cookieJar->getCookies() as $cookie) {
140
			$cookieString .= "{$cookie->getName()}={$cookie->getValue()}; ";
141
		}
142
//		array_map( function ( Cookie $cookie ) use ( $cookieString ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143
//			$cookieString .= "{$cookie->getName()}={$cookie->getValue()}; ";
144
//		}, $this->cookieJar->getCookies() );
145
146
		return $cookieString;
147
	}
148
149
	/**
150
	 * @param $method
151
	 * @param $url
152
	 * @param array $parameters
153
	 *
154
	 * @return MessageInterface
155
	 */
156
	public function createRequest( $method, $url, $parameters = [] ) {
157
		// Create request from URL
158
		$request = new Request( $method );
159
		$request->fromUrl( new Url( $url ) );
160
161
162
		// Add headers
163
		$request->setHeaders( [
164
			'X-Context'           => $this->currentToken,
165
			'X-Prototype-Version' => '1.7',
166
			'Content-Type'        => 'application/x-sah-ws-1-call+json; charset=UTF-8',
167
			'Accept'              => 'text/javascript',
168
		] );
169
170
		// Add cookie header
171
		$request->addHeader( $this->getCookieHeaderForRequest() );
172
173
		// Set content
174
		$request->setContent( json_encode( $parameters ) );
175
176
		$response = $this->browser->send( $request );
177
178
		return $response;
179
	}
180
181
	/**
182
	 * @return \Buzz\Util\CookieJar
183
	 */
184
	public function getCookieJar() {
185
		return $this->cookieJar;
186
	}
187
188
	/**
189
	 * @param $host
190
	 */
191
	public function logout( $host ) {
192
		$response = $this->browser->post( "$host/logout" );
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
193
	}
194
195
}