Completed
Push — master ( b87705...a9c0ba )
by Peter
03:21
created

Auth::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace PeterColes\Betfair\Api;
4
5
use Exception;
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
     * API fail status
21
     */
22
    const API_STATUS_FAIL = 'FAIL';
23
24
    /**
25
     * Application key, provided by Betfair on registration
26
     */
27
    public static $appKey = null;
28
29
    /**
30
     * Session token, provided by Betfair at login
31
     */
32
    public static $sessionToken = null;
33
34
    /**
35
     * Time of last login, expressed in seconds since the Unix Epoch
36
     */
37
    public static $lastLogin = null;
38
39
    /**
40
     * Wrapper method for other methods to initiate and manage a Betfair session
41
     *
42
     * @param  string $appKey
43
     * @param  string $username
44
     * @param  string $password
45
     */
46
    public function init($appKey, $username, $password)
47
    {
48
        if ($appKey == self::$appKey && $this->sessionRemaining() > 5) {
49
            $this->keepAlive();
50
        } else {
51
            self::$appKey = $appKey;
52
            self::$sessionToken = $this->login($appKey, $username, $password);
53
        }
54
    }
55
56
    /**
57
     * Accept app key and session token and extend session
58
     *
59
     * @param  string $appKey
60
     * @param  string $sessionToken
61
     * @return string
62
     */
63
    public function persist($appKey, $sessionToken)
64
    {
65
        if (!$sessionToken) {
66
            throw new Exception('Invalid session token');
67
        }
68
69
        self::$appKey = $appKey;
70
        self::$sessionToken = $sessionToken;
71
72
        return $this->keepAlive();
73
    }
74
75
    /**
76
     * Method to directly execute Betfair login request.
77
     * For use only when the init() method isn't appropriate
78
     *
79
     * @param  string $appKey
80
     * @param  string $username
81
     * @param  string $password
82
     * @return string
83
     * @throws Exception
84
     */
85
    public function login($appKey, $username, $password)
86
    {
87
        self::$appKey = $appKey;
88
89
        $request = $this->httpClient
90
            ->setMethod('post')
91
            ->setEndPoint(self::ENDPOINT.'login/')
92
            ->setFormData([ 'username' => $username, 'password' => $password ]);
93
94
        $result = $this->execute($request);
95
96
        self::$lastLogin = time();
97
98
        return $result->token;
99
    }
100
101
    /**
102
     * Execute Betfair API call to extend the current session
103
     *
104
     * @return string
105
     * @throws Exception
106
     */
107
    public function keepAlive()
108
    {
109
        $result = $this->execute($this->httpClient->setEndPoint(self::ENDPOINT.'keepAlive/'));
110
111
        self::$lastLogin = time();
112
113
        return $result->token;
114
    }
115
116
    /**
117
     * Execute Betfair API call to logout from their system.
118
     * Clear all local references to the session.
119
     *
120
     * @throws Exception
121
     */
122
    public function logout()
123
    {
124
        $result = $this->execute($this->httpClient->setEndPoint(self::ENDPOINT.'logout/'));
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
125
126
        self::$appKey = null;
127
        self::$sessionToken = null;
128
        self::$lastLogin = null;
129
    }
130
131
    /**
132
     * Calculate and provide the time remaining until the current session token expires
133
     *
134
     * @return integer
135
     */
136
    public function sessionRemaining()
137
    {
138
        if (self::$sessionToken === null) {
139
            return 0;
140
        }
141
142
        return self::$lastLogin + self::SESSION_LENGTH - time();
143
    }
144
145
    public function execute($request)
146
    {
147
        $result = $request->authHeaders()->send();
148
149
        if ($result->status === self::API_STATUS_FAIL) {
150
            throw new Exception('Error: '.$result->error);
151
        }
152
153
        return $result;
154
    }
155
}
156