1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSW\SiftScience\Services; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Resource\Collection; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use League\Fractal\Serializer\ArraySerializer; |
8
|
|
|
use League\Fractal\TransformerAbstract; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use WSW\SiftScience\Client\Client; |
11
|
|
|
use WSW\SiftScience\Credentials; |
12
|
|
|
use League\Fractal\Manager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseService |
16
|
|
|
* |
17
|
|
|
* @package WSW\SiftScience\Services |
18
|
|
|
* @author Ronaldo Matos Rodrigues <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class BaseService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const ENDPOINT = '/v204/events'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Credentials |
29
|
|
|
*/ |
30
|
|
|
protected $credentials; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Manager; |
34
|
|
|
*/ |
35
|
|
|
protected $fractal; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Client |
39
|
|
|
*/ |
40
|
|
|
private $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Credentials $credentials |
44
|
|
|
* @param Client $client |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Credentials $credentials, Client $client = null) |
47
|
|
|
{ |
48
|
|
|
$this->credentials = $credentials; |
49
|
|
|
$this->client = $client ?: new Client(); |
50
|
|
|
$this->fractal = new Manager(); |
51
|
|
|
$this->fractal->setSerializer(new ArraySerializer()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $resource |
56
|
|
|
* @param array $params |
57
|
|
|
* |
58
|
|
|
* @return ResponseInterface |
59
|
|
|
*/ |
60
|
|
|
protected function get($resource, array $params = []) |
61
|
|
|
{ |
62
|
|
|
return $this->client->get($this->credentials->getWsUrl($resource, $params)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $resource |
67
|
|
|
* @param string $request |
68
|
|
|
* |
69
|
|
|
* @return ResponseInterface |
70
|
|
|
*/ |
71
|
|
|
protected function post($resource, $request) |
72
|
|
|
{ |
73
|
|
|
return $this->client->post($this->credentials->getWsUrl($resource), $request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $data |
78
|
|
|
* @param TransformerAbstract $transformer |
79
|
|
|
* |
80
|
|
|
* @return \League\Fractal\Scope |
81
|
|
|
*/ |
82
|
|
|
protected function item($data = null, TransformerAbstract $transformer = null) |
83
|
|
|
{ |
84
|
|
|
return $this->fractal->createData(new Item($data, $transformer)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $data |
89
|
|
|
* @param TransformerAbstract $transformer |
90
|
|
|
* |
91
|
|
|
* @return \League\Fractal\Scope |
92
|
|
|
*/ |
93
|
|
|
protected function collection($data = null, TransformerAbstract $transformer = null) |
94
|
|
|
{ |
95
|
|
|
return $this->fractal->createData(new Collection($data, $transformer)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|