LoginDialog::getQueryParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * LoginDialog.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		http://www.ipublikuj.eu
7
 * @author		Adam Kadlec http://www.ipublikuj.eu
8
 * @package		iPublikuj:Twitter!
9
 * @subpackage	UI
10
 * @since		5.0
11
 *
12
 * @date		01.03.15
13
 */
14
15
namespace IPub\Twitter\UI;
16
17
use Nette;
18
use Nette\Application;
19
use Nette\Http;
20
21
use IPub;
22
use IPub\Twitter;
23
use IPub\Twitter\Exceptions;
24
25
use IPub\OAuth;
26
27
/**
28
 * Component that you can connect to presenter
29
 * and use as public mediator for Twitter OAuth redirects communication
30
 *
31
 * @package		iPublikuj:Twitter!
32
 * @subpackage	UI
33
 *
34
 * @author Adam Kadlec <[email protected]>
35
 *
36
 * @method onResponse(LoginDialog $dialog)
37
 * @method onDialogNotOpened(LoginDialog $dialog, Exception $ex)
38
 */
39
class LoginDialog extends Application\UI\Control
40
{
41
	/**
42
	 * @var array of function(LoginDialog $dialog)
43
	 */
44
	public $onResponse = [];
45
46
	/**
47
	 * @var \Closure[]
48
	 */
49
	public $onDialogNotOpened = [];
50
51
	/**
52
	 * @var Twitter\Client
53
	 */
54
	protected $client;
55
56
	/**
57
	 * @var Twitter\Configuration
58
	 */
59
	protected $config;
60
61
	/**
62
	 * @var Twitter\SessionStorage
63
	 */
64
	protected $session;
65
66
	/**
67
	 * @param Twitter\Client $twitter
68
	 */
69
	public function __construct(Twitter\Client $twitter)
70
	{
71
		$this->client = $twitter;
72
		$this->config = $twitter->getConfig();
73
		$this->session = $twitter->getSession();
74
75
		parent::__construct();
76
77
		$this->monitor('Nette\Application\IPresenter');
78
	}
79
80
	/**
81
	 * @return Twitter\Client
82
	 */
83
	public function getClient()
84
	{
85
		return $this->client;
86
	}
87
88
	/**
89
	 * @param Nette\ComponentModel\Container $obj
90
	 */
91
	protected function attached($obj)
92
	{
93
		parent::attached($obj);
94
95
		if ($obj instanceof Nette\Application\IPresenter) {
96
			$this->client->getConsumer()->setCallbackUrl(new Http\UrlScript($this->link('//response!')));
97
		}
98
	}
99
100
	public function handleCallback()
101
	{
102
103
	}
104
105
	/**
106
	 * Checks, if there is a user in storage and if not, it redirects to login dialog.
107
	 * If the user is already in session storage, it will behave, as if were redirected from twitter right now,
108
	 * this means, it will directly call onResponse event.
109
	 *
110
	 * @throws Nette\Application\AbortException
111
	 */
112
	public function handleOpen()
113
	{
114
		if (!$this->client->getUser()) { // no user
115
			try {
116
				$this->open();
117
			} catch (OAuth\Exceptions\IException $ex) {
118
				$this->onDialogNotOpened($this, $ex);
0 ignored issues
show
Documentation introduced by
$ex is of type object<IPub\OAuth\Exceptions\IException>, but the function expects a object<IPub\Twitter\UI\Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
				throw $ex;
120
			}
121
		}
122
123
		$this->onResponse($this);
124
		$this->presenter->redirect('this');
125
	}
126
127
	/**
128
	 * @throws Nette\Application\AbortException
129
	 * @throws OAuth\Exceptions\RequestFailedException
130
	 */
131
	public function open()
132
	{
133
		if ($this->client->obtainRequestToken()) {
134
			$this->presenter->redirectUrl($this->getUrl());
135
136
		} else {
137
			throw new OAuth\Exceptions\RequestFailedException(sprintf('User could not be authenticated to "%s".', 'twitter'));
138
		}
139
	}
140
141
	/**
142
	 * @return array
143
	 */
144
	public function getQueryParams()
145
	{
146
		$params = [
147
			'oauth_token' => $this->session->request_token
148
		];
149
150
		return $params;
151
	}
152
153
	/**
154
	 * @return string
155
	 */
156
	public function getUrl()
157
	{
158
		return (string) $this->config->createUrl('oauth', 'authenticate', $this->getQueryParams());
159
	}
160
161
	public function handleResponse()
162
	{
163
		$this->client->getUser(); // check the received parameters and save user
164
		$this->onResponse($this);
165
		$this->presenter->redirect('this', ['oauth_token' => NULL, 'oauth_verifier' => NULL]);
166
	}
167
}