1
|
|
|
<?php namespace Cornford\Packtpublr; |
2
|
|
|
|
3
|
|
|
use Cornford\Packtpublr\Contracts\RedeemableInterface; |
4
|
|
|
use Cornford\Packtpublr\Exceptions\PacktpublrRedeemException; |
5
|
|
|
use Cornford\Packtpublr\Exceptions\PacktpublrRequestException; |
6
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
7
|
|
|
|
8
|
|
|
class Packtpublr extends PacktpublrBase implements RedeemableInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The base URL. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $baseUrl = 'https://www.packtpub.com'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The login URL. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $loginUrl = '/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The redeem URL. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $redeemUrl = '/packt/offers/free-learning'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The logout URL. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $logoutUrl = '/logout'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The login form fields. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $loginFormFields = [ |
45
|
|
|
'op' => 'Login', |
46
|
|
|
'form_build_id' => 'form-80807b1adfed1b70ff728d255a89cc65', |
47
|
|
|
'form_id' => 'packt_user_login_form', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The login email address. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $email; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The login password. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $password; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Login. |
66
|
|
|
* |
67
|
|
|
* @param string $email |
68
|
|
|
* @param string $password |
69
|
|
|
* |
70
|
|
|
* @throws PacktpublrRequestException |
71
|
|
|
* |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
|
|
public function login($email = null, $password = null) |
75
|
|
|
{ |
76
|
|
|
$authenticationCredentials = [ |
77
|
|
|
'email' => ($email ?: $this->email), |
78
|
|
|
'password' => ($password ?: $this->password) |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
return $this->createRequest( |
82
|
|
|
$this->loginUrl, |
83
|
|
|
array_merge($authenticationCredentials, $this->loginFormFields), |
84
|
|
|
self::REQUEST_METHOD_POST |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Redeem. |
90
|
|
|
* |
91
|
|
|
* @throws PacktpublrRequestException |
92
|
|
|
* @throws PacktpublrRedeemException |
93
|
|
|
* |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
|
|
public function redeem() |
97
|
|
|
{ |
98
|
|
|
$this->createRequest( |
99
|
|
|
'/packt/offers/free-learning' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
preg_match('/<a href="(\/freelearning-claim\/[0-9]+\/[0-9]+)" class="twelve-days-claim">/', $this->getResponseBody(), $matches); |
103
|
|
|
|
104
|
|
|
if (empty($matches)) { |
105
|
|
|
throw new PacktpublrRedeemException('An error locating an item to redeem.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->createRequest( |
109
|
|
|
$matches[1], |
110
|
|
|
[], |
111
|
|
|
self::REQUEST_METHOD_POST |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Logout. |
117
|
|
|
* |
118
|
|
|
* @throws PacktpublrRequestException |
119
|
|
|
* |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function logout() |
123
|
|
|
{ |
124
|
|
|
$result = $this->createRequest( |
125
|
|
|
'/logout' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->cookieSubscriber->getCookieJar()->clearSessionCookies(); |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Run. |
135
|
|
|
* |
136
|
|
|
* @param string $email |
137
|
|
|
* @param string $password |
138
|
|
|
* |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
|
|
public function run($email = null, $password = null) |
142
|
|
|
{ |
143
|
|
|
$result = false; |
144
|
|
|
|
145
|
|
|
$this->line('Attempting to redeem current free-learning item from PactPub'); |
146
|
|
|
|
147
|
|
|
$this->line('Logging in using URL "<fg=cyan>' . $this->baseUrl . $this->loginUrl . '</>"...'); |
148
|
|
|
|
149
|
|
|
if ($response = $this->login($email, $password)) { |
150
|
|
|
$this->success(' Success'); |
151
|
|
|
} else { |
152
|
|
|
$this->error(' Error'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$result |= $response; |
156
|
|
|
|
157
|
|
|
$this->line('Redeeming current free-learning item using URL "<fg=cyan>' . $this->baseUrl . $this->redeemUrl . '</>"...'); |
158
|
|
|
|
159
|
|
|
if ($response = $this->redeem()) { |
160
|
|
|
$this->success(' Success'); |
161
|
|
|
} else { |
162
|
|
|
$this->error(' Error'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$result |= $response; |
166
|
|
|
|
167
|
|
|
$this->line('Logging out of using using URL "<fg=cyan>' . $this->baseUrl . $this->logoutUrl . '</>"...'); |
168
|
|
|
|
169
|
|
|
if ($response = $this->logout()) { |
170
|
|
|
$this->success(' Success'); |
171
|
|
|
} else { |
172
|
|
|
$this->error(' Error'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$result |= $response; |
176
|
|
|
|
177
|
|
|
return (boolean) $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |