InstapushClient::getHttpClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /** @var bool */
36
    private $handleException = true;
37
38
    /**
39
     * @param \Http\Client\HttpClient                               $httpClient
40
     * @param \Http\Message\RequestFactory                          $requestFactory
41
     * @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer
42
     */
43
    public function __construct(
44
        HttpClient $httpClient,
45
        RequestFactory $requestFactory = null,
46
        TransformerInterface $transformer = null
47
    ) {
48
        $this->httpClient = $httpClient;
49
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
50
        $this->transformer = $transformer ?: new ModelTransformer();
51
    }
52
53
    /**
54
     * @param \Fnayou\InstapushPHP\Http\HttpClientConfiguratorInterface $httpClientConfigurator
55
     * @param \Http\Message\RequestFactory                              $requestFactory
56
     * @param \Fnayou\InstapushPHP\Transformer\TransformerInterface     $transformer
57
     *
58
     * @return $this
59
     */
60
    public static function configure(
61
        HttpClientConfiguratorInterface $httpClientConfigurator,
62
        RequestFactory $requestFactory = null,
63
        TransformerInterface $transformer = null
64
    ) {
65
        $httpClient = $httpClientConfigurator->createConfiguredClient();
66
67
        return new static($httpClient, $requestFactory, $transformer);
68
    }
69
70
    /**
71
     * @param string $userToken
72
     * @param string $appIdentifier
73
     * @param string $appSecret
74
     *
75
     * @return $this
76
     */
77
    public static function create(string $userToken = null, string $appIdentifier = null, string $appSecret = null)
78
    {
79
        $httpClientConfigurator = new HttpClientConfigurator();
80
        $httpClientConfigurator
81
            ->setApiUserToken($userToken)
82
            ->setApiAppIdentifier($appIdentifier)
83
            ->setApiAppSecret($appSecret);
84
85
        return static::configure($httpClientConfigurator);
86
    }
87
88
    /**
89
     * @param string $userToken
90
     *
91
     * @return $this
92
     */
93
    public static function createForUser(string $userToken)
94
    {
95
        $httpClientConfigurator = new HttpClientConfigurator();
96
        $httpClientConfigurator
97
            ->setApiUserToken($userToken);
98
99
        return static::configure($httpClientConfigurator);
100
    }
101
102
    /**
103
     * @param string $appIdentifier
104
     * @param string $appSecret
105
     *
106
     * @return $this
107
     */
108
    public static function createForApp(string $appIdentifier, string $appSecret)
109
    {
110
        $httpClientConfigurator = new HttpClientConfigurator();
111
        $httpClientConfigurator
112
            ->setApiAppIdentifier($appIdentifier)
113
            ->setApiAppSecret($appSecret);
114
115
        return static::configure($httpClientConfigurator);
116
    }
117
118
    /**
119
     * @return \Http\Client\HttpClient
120
     */
121
    public function getHttpClient()
122
    {
123
        return $this->httpClient;
124
    }
125
126
    /**
127
     * @param \Http\Client\HttpClient $httpClient
128
     */
129
    public function setHttpClient(HttpClient $httpClient)
130
    {
131
        $this->httpClient = $httpClient;
132
    }
133
134
    /**
135
     * @return \Http\Message\RequestFactory
136
     */
137
    public function getRequestFactory()
138
    {
139
        return $this->requestFactory;
140
    }
141
142
    /**
143
     * @param \Http\Message\RequestFactory $requestFactory
144
     */
145
    public function setRequestFactory(RequestFactory $requestFactory)
146
    {
147
        $this->requestFactory = $requestFactory;
148
    }
149
150
    /**
151
     * @return \Fnayou\InstapushPHP\Transformer\TransformerInterface
152
     */
153
    public function getTransformer()
154
    {
155
        return $this->transformer;
156
    }
157
158
    /**
159
     * @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer
160
     */
161
    public function setTransformer(TransformerInterface $transformer)
162
    {
163
        $this->transformer = $transformer;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isHandleException()
170
    {
171
        return $this->handleException;
172
    }
173
174
    /**
175
     * @param bool $handleException
176
     */
177
    public function setHandleException(bool $handleException)
178
    {
179
        $this->handleException = $handleException;
180
    }
181
182
    /**
183
     * @return Api\ApplicationsApi
184
     */
185
    public function applications()
186
    {
187
        return new Api\ApplicationsApi($this);
188
    }
189
190
    /**
191
     * @return Api\EventsApi
192
     */
193
    public function events()
194
    {
195
        return new Api\EventsApi($this);
196
    }
197
198
    /**
199
     * @return Api\NotificationApi
200
     */
201
    public function notification()
202
    {
203
        return new Api\NotificationApi($this);
204
    }
205
}
206