Completed
Pull Request — master (#3)
by Aymen
02:00
created

InstapushClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 5
dl 0
loc 136
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A configure() 0 9 1
A create() 0 10 1
A getHttpClient() 0 4 1
A setHttpClient() 0 4 1
A getRequestFactory() 0 4 1
A setRequestFactory() 0 4 1
A getTransformer() 0 4 1
A setTransformer() 0 4 1
A isException() 0 4 1
A setException() 0 4 1
A applications() 0 4 1
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 $exception = 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
     * @return \Http\Client\HttpClient
90
     */
91
    public function getHttpClient()
92
    {
93
        return $this->httpClient;
94
    }
95
96
    /**
97
     * @param \Http\Client\HttpClient $httpClient
98
     */
99
    public function setHttpClient(HttpClient $httpClient)
100
    {
101
        $this->httpClient = $httpClient;
102
    }
103
104
    /**
105
     * @return \Http\Message\RequestFactory
106
     */
107
    public function getRequestFactory()
108
    {
109
        return $this->requestFactory;
110
    }
111
112
    /**
113
     * @param \Http\Message\RequestFactory $requestFactory
114
     */
115
    public function setRequestFactory(RequestFactory $requestFactory)
116
    {
117
        $this->requestFactory = $requestFactory;
118
    }
119
120
    /**
121
     * @return \Fnayou\InstapushPHP\Transformer\TransformerInterface
122
     */
123
    public function getTransformer()
124
    {
125
        return $this->transformer;
126
    }
127
128
    /**
129
     * @param \Fnayou\InstapushPHP\Transformer\TransformerInterface $transformer
130
     */
131
    public function setTransformer(TransformerInterface $transformer)
132
    {
133
        $this->transformer = $transformer;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function isException()
140
    {
141
        return $this->exception;
142
    }
143
144
    /**
145
     * @param bool $exception
146
     */
147
    public function setException(bool $exception)
148
    {
149
        $this->exception = $exception;
150
    }
151
152
    /**
153
     * @return Api\ApplicationsApi
154
     */
155
    public function applications()
156
    {
157
        return new Api\ApplicationsApi($this);
158
    }
159
}
160