-
Notifications
You must be signed in to change notification settings - Fork 17
/
Session.php
58 lines (48 loc) · 1.32 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace ValdeirPsr\PagSeguro\Request;
use DOMDocument;
use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException;
class Session
{
private $env;
public function __construct(Environment $env)
{
$this->env = $env;
}
/**
* Gera uma sessão
*
* @throws AuthException Caso as credenciais sejam inválidas
*
* @return string
*/
public function generate(): string
{
$url = $this->buildUrl();
$request = Factory::request($this->env);
$request->post($url);
$request->close();
if ($request->isSuccess()) {
$xml = $request->getResponse();
$dom = new DOMDocument();
$dom->loadXml($xml);
$session = $dom->getElementsByTagName('id');
if ($session->count() > 0) {
return trim($session->item(0)->textContent);
}
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
}
}
/**
* {@inheritDoc}
*/
protected function buildUrl(): string
{
return Factory::url($this->env, 'v2/sessions', [
'email' => $this->env->getEmail(),
'token' => $this->env->getToken()
]);
}
}