1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Audiens\BeesWax; |
5
|
|
|
|
6
|
|
|
use Audiens\BeesWax\Exception\BeesWaxGenericException; |
7
|
|
|
use Audiens\BeesWax\Exception\BeesWaxLoginException; |
8
|
|
|
use Audiens\BeesWax\Exception\BeesWaxResponseException; |
9
|
|
|
|
10
|
|
|
class BeesWaxSession |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
private $buzzKey; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $email; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $password; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $cookiesContent; |
23
|
|
|
|
24
|
|
|
/** @var BeesWaxRequestBuilder */ |
25
|
|
|
private $requestBuilder; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
private $loggedIn; |
29
|
|
|
|
30
|
|
|
public function __construct(string $buzzKey, string $email, string $password, string $cookiesContent = '') |
31
|
|
|
{ |
32
|
|
|
$this->buzzKey = $buzzKey; |
33
|
|
|
$this->email = $email; |
34
|
|
|
$this->password = $password; |
35
|
|
|
$this->cookiesContent = $cookiesContent; |
36
|
|
|
|
37
|
|
|
$this->loggedIn = false; |
38
|
|
|
$this->requestBuilder = new BeesWaxRequestBuilder($this); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setRequestBuilder(BeesWaxRequestBuilder $requestBuilder): BeesWaxSession |
42
|
|
|
{ |
43
|
|
|
$this->requestBuilder = $requestBuilder; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param bool $longerLastingSession |
50
|
|
|
* @param bool $forceRefresh |
51
|
|
|
* |
52
|
|
|
* @see https://docs.beeswax.com/v0.5/docs/authentication |
53
|
|
|
* |
54
|
|
|
* @throws BeesWaxLoginException |
55
|
|
|
* @throws BeesWaxResponseException |
56
|
|
|
* @throws BeesWaxGenericException |
57
|
|
|
*/ |
58
|
|
|
public function login(bool $longerLastingSession = true, bool $forceRefresh = false): void |
59
|
|
|
{ |
60
|
|
|
if (!$forceRefresh && $this->loggedIn && $this->cookiesContent) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$payload = [ |
65
|
|
|
'email' => $this->email, |
66
|
|
|
'password' => $this->password, |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
if ($longerLastingSession) { |
70
|
|
|
$payload['keep_logged_in'] = 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$request = $this->requestBuilder->build( |
74
|
|
|
'/rest/authenticate', |
75
|
|
|
[], |
76
|
|
|
BeesWaxRequest::METHOD_POST, |
77
|
|
|
json_encode($payload) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$response = $request->doRequest(); |
81
|
|
|
|
82
|
|
|
$json = json_decode($response->getPayload()); |
83
|
|
|
|
84
|
|
|
if ($json === false || $json->success !== true) { |
85
|
|
|
throw new BeesWaxLoginException($response, 'Impossible to login with the given information'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->loggedIn = true; |
89
|
|
|
$this->cookiesContent = $response->getCookiesContent(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getSessionCookies(): string |
93
|
|
|
{ |
94
|
|
|
return $this->cookiesContent; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getBuzzKey(): string |
98
|
|
|
{ |
99
|
|
|
return $this->buzzKey; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getRequestBuilder(): BeesWaxRequestBuilder |
103
|
|
|
{ |
104
|
|
|
return $this->requestBuilder; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|