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\HttpClientConfigurator; |
14
|
|
|
use Fnayou\InstapushPHP\Transformer\ModelTransformer; |
15
|
|
|
use Fnayou\InstapushPHP\Transformer\TransformerInterface; |
16
|
|
|
use Http\Client\HttpClient; |
17
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
18
|
|
|
use Http\Message\RequestFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class InstapushClient. |
22
|
|
|
*/ |
23
|
|
|
final class InstapushClient |
24
|
|
|
{ |
25
|
|
|
/** @var \Http\Client\HttpClient */ |
26
|
|
|
private $httpClient; |
27
|
|
|
|
28
|
|
|
/** @var \Http\Message\RequestFactory */ |
29
|
|
|
private $requestFactory; |
30
|
|
|
|
31
|
|
|
/** @var \Fnayou\InstapushPHP\Transformer\TransformerInterface */ |
32
|
|
|
private $transformer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \Http\Client\HttpClient $httpClient |
36
|
|
|
* @param \Http\Message\RequestFactory $requestFactory |
37
|
|
|
* @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
HttpClient $httpClient, |
41
|
|
|
RequestFactory $requestFactory = null, |
42
|
|
|
TransformerInterface $transformer = null |
43
|
|
|
) { |
44
|
|
|
$this->httpClient = $httpClient; |
45
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
46
|
|
|
$this->transformer = $transformer ?: new ModelTransformer(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param \Fnayou\InstapushPHP\HttpClientConfigurator $httpClientConfigurator |
51
|
|
|
* @param \Http\Message\RequestFactory $requestFactory |
52
|
|
|
* @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public static function configure( |
57
|
|
|
HttpClientConfigurator $httpClientConfigurator, |
58
|
|
|
RequestFactory $requestFactory = null, |
59
|
|
|
TransformerInterface $transformer = null |
60
|
|
|
) { |
61
|
|
|
$httpClient = $httpClientConfigurator->createConfiguredClient(); |
62
|
|
|
|
63
|
|
|
return new static($httpClient, $requestFactory, $transformer); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $userToken |
68
|
|
|
* @param string $appIdentifier |
69
|
|
|
* @param string $appSecret |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public static function create(string $userToken, string $appIdentifier, string $appSecret) |
74
|
|
|
{ |
75
|
|
|
$httpClientConfigurator = new HttpClientConfigurator(); |
76
|
|
|
$httpClientConfigurator->setApiCredentials($userToken, $appIdentifier, $appSecret); |
77
|
|
|
|
78
|
|
|
return static::configure($httpClientConfigurator); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|