1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Betfair\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Command\Guzzle\Description; |
7
|
|
|
use GuzzleHttp\Command\Guzzle\GuzzleClient; |
8
|
|
|
use Betfair\Client\Guzzle\Location\ResponseLocation; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class BetfairGuzzleClientFactory |
13
|
|
|
{ |
14
|
|
|
private $specificationDir; |
15
|
|
|
|
16
|
|
|
public function __construct($specificationDir = null) |
17
|
|
|
{ |
18
|
|
|
$this->specificationDir = $specificationDir !== null |
19
|
|
|
? $specificationDir |
20
|
|
|
:__DIR__."/../Resources/specification"; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function createBetfairGuzzleClient($options = array()) |
24
|
|
|
{ |
25
|
|
|
$httpClient = new Client(); |
26
|
|
|
|
27
|
|
|
$description = new Description( |
28
|
|
|
$this->getConfigDescriptionArrayFromSpecificationDir($this->specificationDir, $options) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$guzzleClient = new GuzzleClient($httpClient, $description, [ |
32
|
|
|
'response_locations' => ['response' => new ResponseLocation('response')], //custom json location |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
return new BetfairGuzzleClient($guzzleClient); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getConfigDescriptionArrayFromSpecificationDir($specificationDir, $options = array()) |
39
|
|
|
{ |
40
|
|
|
$guzzleHeaderSpec = file_get_contents(sprintf("%s/api_version_description.yml", $specificationDir)); |
41
|
|
|
$yamlLoader = new Yaml(); |
42
|
|
|
$headersArray = $yamlLoader->parse($guzzleHeaderSpec); |
43
|
|
|
$headersArray = $this->mergeUserHeaderOptions($headersArray, $options); |
44
|
|
|
|
45
|
|
|
$headersArray['operations'] = array(); |
46
|
|
|
$finder = new Finder(); |
47
|
|
|
$finder->files()->in($specificationDir)->notName("api_version_description.yml"); |
48
|
|
|
|
49
|
|
|
foreach ($finder as $file) { |
50
|
|
|
$spec = $yamlLoader->parse(file_get_contents($file->getRealPath())); |
51
|
|
|
foreach ($spec['operations'] as $key => $operationArray) { |
52
|
|
|
if ($key == "betfairLogin") { |
53
|
|
|
$operationArray = $this->mergeUserLoginOptions($operationArray, $options); |
54
|
|
|
} |
55
|
|
|
$headersArray['operations'][$key] = $operationArray; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($spec['models'] as $key => $operationModelArray) { |
59
|
|
|
$headersArray['models'][$key] = $operationModelArray; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $headersArray; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function mergeUserHeaderOptions($headersArray, array $options) |
67
|
|
|
{ |
68
|
|
|
if (isset($options['baseUrl'])) { |
69
|
|
|
$headersArray['baseUrl'] = $options['baseUrl']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $headersArray; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function mergeUserLoginOptions($operationArray, array $options) |
76
|
|
|
{ |
77
|
|
|
if (isset($options['loginEndpoint'])) { |
78
|
|
|
$operationArray['uri'] = $options['loginEndpoint']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $operationArray; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|