Completed
Push — master ( e749c4...0ba39a )
by Peter
02:21
created

Auth::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace PeterColes\Betfair\Api;
4
5
use PeterColes\Betfair\Http\Client as HttpClient;
6
7
class Auth
8
{
9
    const ENDPOINT = 'https://identitysso.betfair.com/api/';
10
11
    protected $httpClient;
12
13
    public static $appKey;
14
15
    public static $sessionToken;
16
17
    public function __construct(HttpClient $httpClient = null)
18
    {
19
        $this->httpClient = $httpClient ?: new HttpClient;
20
    }
21
22
    public function init($appKey, $username, $password)
23
    {
24
        self::$appKey = $appKey;
25
        self::$sessionToken = $this->login($appKey, $username, $password);
26
    }
27
28
    public function login($appKey, $username, $password)
29
    {
30
        $result = $this->httpClient
31
            ->setMethod('post')
32
            ->setEndPoint(self::ENDPOINT.'login/')
33
            ->authHeaders([ 'X-Application' => $appKey ])
34
            ->setFormData([ 'username' => $username, 'password' => $password ])
35
            ->send();
36
37
        return $result->token;
38
    }
39
40
    public function keepAlive()
41
    {
42
        $this->httpClient
43
            ->setEndPoint(self::ENDPOINT.'keepAlive/')
44
            ->authHeaders()
45
            ->send();
46
    }
47
48
    public function logout()
49
    {
50
        $this->httpClient
51
            ->setEndPoint(self::ENDPOINT.'logout/')
52
            ->authHeaders()
53
            ->send();
54
    }
55
}
56