Completed
Push — master ( 7062ee...c98379 )
by Peter
06:27
created

Auth::persist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace PeterColes\Betfair\Api;
4
5
use PeterColes\Betfair\Api\BaseApi;
6
7
class Auth extends BaseApi
8
{
9
    /**
10
     * Betfair API endpoint for authentication requests
11
     */
12
    const ENDPOINT = 'https://identitysso.betfair.com/api/';
13
14
    /**
15
     * 4 hours, expressed in seconds
16
     */
17
    const SESSION_LENGTH = 4 * 60 * 60;
18
19
    /**
20
     * Application key, provided by Betfair on registration
21
     */
22
    public static $appKey = null;
23
24
    /**
25
     * Session token, provided by Betfair at login
26
     */
27
    public static $sessionToken = null;
28
29
    /**
30
     * Time of last login, expressed in seconds since the Unix Epoch
31
     */
32
    public static $lastLogin = null;
33
34
    /**
35
     * Wrapper method for other methods to initiate and manage a Betfair session
36
     *
37
     * @param  string $appKey
38
     * @param  string $username
39
     * @param  string $password
40
     */
41
    public function init($appKey, $username, $password)
42
    {
43
        if ($appKey == self::$appKey && $this->sessionRemaining() > 5) {
44
            $this->keepAlive();
45
        } else {
46
            self::$appKey = $appKey;
47
            self::$sessionToken = $this->login($appKey, $username, $password);
48
        }
49
    }
50
51
    /**
52
     * Accept app key and session token and extend session
53
     *
54
     * @param  string $appKey
55
     * @param  string $sessionToken
56
     */
57
    public function persist($appKey, $sessionToken)
58
    {
59
        self::$appKey = $appKey;
60
        self::$sessionToken = $sessionToken;
61
        $this->keepAlive();
62
    }
63
64
    /**
65
     * Method to directly execute Betfair login request.
66
     * For use only when the init() method isn't appropriate
67
     *
68
     * @param  string $appKey
69
     * @param  string $username
70
     * @param  string $password
71
     */
72
    public function login($appKey, $username, $password)
73
    {
74
        $result = $this->httpClient
75
            ->setMethod('post')
76
            ->setEndPoint(self::ENDPOINT.'login/')
77
            ->authHeaders([ 'X-Application' => $appKey ])
78
            ->setFormData([ 'username' => $username, 'password' => $password ])
79
            ->send();
80
81
        self::$lastLogin = time();
82
83
        return $result->token;
84
    }
85
86
    /**
87
     * Execute Betfair API call to extend the current session
88
     */
89
    public function keepAlive()
90
    {
91
        $this->httpClient
92
            ->setEndPoint(self::ENDPOINT.'keepAlive/')
93
            ->authHeaders()
94
            ->send();
95
96
        self::$lastLogin = time();
97
    }
98
99
    /**
100
     * Execute Betfair API call to logout from their system.
101
     * Clear all local references to the session.
102
     */
103
    public function logout()
104
    {
105
        $this->httpClient
106
            ->setEndPoint(self::ENDPOINT.'logout/')
107
            ->authHeaders()
108
            ->send();
109
110
        self::$appKey = null;
111
        self::$sessionToken = null;
112
        self::$lastLogin = null;
113
    }
114
115
    /**
116
     * Calculate and provide the time remaining until the current session token expires
117
     */
118
    public function sessionRemaining()
119
    {
120
        return self::$lastLogin + self::SESSION_LENGTH - time();
121
    }
122
}
123