BetfairClient::authenticateCredential()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Betfair library.
4
 *
5
 * (c) Daniele D'Angeli <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Betfair\Client;
11
12
use Betfair\BetfairActionType;
13
use Betfair\Credential\CredentialInterface;
14
use Betfair\Exception\BetfairLoginException;
15
use Betfair\Model\ParamInterface;
16
use GuzzleHttp\Message\Response;
17
18
class BetfairClient implements BetfairClientInterface
19
{
20
    const LOGIN_ENDPOINT = "https://identitysso.betfair.com/api/login";
21
22
    /** @var  CredentialInterface */
23
    protected $credential;
24
25
    /** @var  BetfairGuzzleClient $client */
26
    protected $betfairGuzzleClient;
27
28
    public function __construct(CredentialInterface $credential, BetfairGuzzleClient $guzzleClient)
29
    {
30
        $this->credential = $credential;
31
        $this->betfairGuzzleClient = $guzzleClient;
32
    }
33
34
    /**
35
     * @param string $operationName operation name
36
     * @param ParamInterface $param Param to be serialized in the request
37
     * @param string $type
38
     * @return string $bodyString
39
     */
40
    public function apiNgRequest($operationName, ParamInterface $param, $type = "betting")
41
    {
42
        $requestParameters = array_merge(
43
                $this->getDefaultAuthHeaderArray(),
44
                $this->builtJsonRpcArrayParameters($operationName, $param, $type),
45
                array("type" => $type)
46
        );
47
48
        $response = $this->betfairGuzzleClient->apiNgRequest(
49
            $requestParameters
50
        );
51
52
        return $response->getBody();
53
    }
54
55
    private function builtJsonRpcArrayParameters($operationName, ParamInterface $param, $type)
56
    {
57
        $methodName = $type === BetfairActionType::BETTING ? "SportsAPING" : "AccountAPING";
58
        $jsonRpcArrayParameters = array();
59
        $jsonRpcArrayParameters['method'] = $methodName . "/v1.0/" . $operationName;
60
        $jsonRpcArrayParameters['params'] = $param;
61
62
        return $jsonRpcArrayParameters;
63
    }
64
65
    /**
66
     * Login and set the sessionToken in Credential object
67
     */
68
    public function authenticateCredential()
69
    {
70
        $sessionToken = $this->login();
71
        $this->credential->setSessionToken($sessionToken);
72
    }
73
74
    /**
75
     * @return string $sessionToken
76
     * @throws \Betfair\Exception\BetfairLoginException
77
     */
78
    private function login()
79
    {
80
        /** @var Response $result */
81
        $result = $this->betfairGuzzleClient->betfairLogin($this->builtLoginArrayParameters());
82
83
        if ($result && $result->getStatusCode() == 200) {
84
            return $this->extractSessionTokenFromResponseBody($result->getBody());
85
        }
86
87
        throw new BetfairLoginException(sprintf("Error during credentials verification."));
88
    }
89
90
91
    private function getDefaultAuthHeaderArray()
92
    {
93
        if (!$this->credential->isAuthenticated()) {
94
            $this->authenticateCredential();
95
        }
96
97
        return array(
98
            "X-Application" => $this->credential->getApplicationKey(),
99
            "X-Authentication" => $this->credential->getSessionToken()
100
        );
101
    }
102
103
    private function builtLoginArrayParameters()
104
    {
105
        return array(
106
            'X-Application' => $this->credential->getApplicationKey(),
107
            'username' => $this->credential->getUsername(),
108
            'password' => $this->credential->getPassword()
109
        );
110
    }
111
112
    private function extractSessionTokenFromResponseBody($responseBody)
113
    {
114
        $bodyArray = json_decode($responseBody, true);
115
116
        if (isset($bodyArray["status"]) && $bodyArray["status"] == "SUCCESS") {
117
            return $bodyArray["token"];
118
        }
119
120
        throw new BetfairLoginException(sprintf("Error during credentials verification."));
121
    }
122
}
123