1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the fnayou/instapush-php project. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017. Aymen FNAYOU <[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
|
|
|
|
11
|
|
|
namespace Fnayou\InstapushPHP; |
12
|
|
|
|
13
|
|
|
use Fnayou\InstapushPHP\Http\HttpClientConfigurator; |
14
|
|
|
use Fnayou\InstapushPHP\Http\HttpClientConfiguratorInterface; |
15
|
|
|
use Fnayou\InstapushPHP\Transformer\ModelTransformer; |
16
|
|
|
use Fnayou\InstapushPHP\Transformer\TransformerInterface; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
19
|
|
|
use Http\Message\RequestFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class InstapushClient. |
23
|
|
|
*/ |
24
|
|
|
final class InstapushClient |
25
|
|
|
{ |
26
|
|
|
/** @var \Http\Client\HttpClient */ |
27
|
|
|
private $httpClient; |
28
|
|
|
|
29
|
|
|
/** @var \Http\Message\RequestFactory */ |
30
|
|
|
private $requestFactory; |
31
|
|
|
|
32
|
|
|
/** @var \Fnayou\InstapushPHP\Transformer\TransformerInterface */ |
33
|
|
|
private $transformer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Http\Client\HttpClient $httpClient |
37
|
|
|
* @param \Http\Message\RequestFactory $requestFactory |
38
|
|
|
* @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
HttpClient $httpClient, |
42
|
|
|
RequestFactory $requestFactory = null, |
43
|
|
|
TransformerInterface $transformer = null |
44
|
|
|
) { |
45
|
|
|
$this->httpClient = $httpClient; |
46
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
47
|
|
|
$this->transformer = $transformer ?: new ModelTransformer(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Fnayou\InstapushPHP\Http\HttpClientConfiguratorInterface $httpClientConfigurator |
52
|
|
|
* @param \Http\Message\RequestFactory $requestFactory |
53
|
|
|
* @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public static function configure( |
58
|
|
|
HttpClientConfiguratorInterface $httpClientConfigurator, |
59
|
|
|
RequestFactory $requestFactory = null, |
60
|
|
|
TransformerInterface $transformer = null |
61
|
|
|
) { |
62
|
|
|
$httpClient = $httpClientConfigurator->createConfiguredClient(); |
63
|
|
|
|
64
|
|
|
return new static($httpClient, $requestFactory, $transformer); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $userToken |
69
|
|
|
* @param string $appIdentifier |
70
|
|
|
* @param string $appSecret |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public static function create(string $userToken = null, string $appIdentifier = null, string $appSecret = null) |
75
|
|
|
{ |
76
|
|
|
$httpClientConfigurator = new HttpClientConfigurator(); |
77
|
|
|
$httpClientConfigurator |
78
|
|
|
->setApiUserToken($userToken) |
79
|
|
|
->setApiAppIdentifier($appIdentifier) |
80
|
|
|
->setApiAppSecret($appSecret); |
81
|
|
|
|
82
|
|
|
return static::configure($httpClientConfigurator); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return \Http\Client\HttpClient |
87
|
|
|
*/ |
88
|
|
|
public function getHttpClient() |
89
|
|
|
{ |
90
|
|
|
return $this->httpClient; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \Http\Client\HttpClient $httpClient |
95
|
|
|
*/ |
96
|
|
|
public function setHttpClient(HttpClient $httpClient) |
97
|
|
|
{ |
98
|
|
|
$this->httpClient = $httpClient; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return \Http\Message\RequestFactory |
103
|
|
|
*/ |
104
|
|
|
public function getRequestFactory() |
105
|
|
|
{ |
106
|
|
|
return $this->requestFactory; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Http\Message\RequestFactory $requestFactory |
111
|
|
|
*/ |
112
|
|
|
public function setRequestFactory(RequestFactory $requestFactory) |
113
|
|
|
{ |
114
|
|
|
$this->requestFactory = $requestFactory; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \Fnayou\InstapushPHP\Transformer\TransformerInterface |
119
|
|
|
*/ |
120
|
|
|
public function getTransformer() |
121
|
|
|
{ |
122
|
|
|
return $this->transformer; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer |
127
|
|
|
*/ |
128
|
|
|
public function setTransformer(TransformerInterface $transformer) |
129
|
|
|
{ |
130
|
|
|
$this->transformer = $transformer; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Api\ApplicationsApi |
135
|
|
|
*/ |
136
|
|
|
public function applications() |
137
|
|
|
{ |
138
|
|
|
return new Api\ApplicationsApi($this); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|