Passed
Pull Request — master (#127)
by Alexandr
02:03
created

Messenger::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger;
6
7
use GuzzleHttp\HandlerStack;
8
use Kerox\Messenger\Http\Client;
9
use Kerox\Messenger\Http\Middleware;
10
use Psr\Http\Client\ClientInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
class Messenger
14
{
15
    public const API_URL = 'https://graph.facebook.com/';
16
    public const API_VERSION = 'v3.1';
17
18
    /**
19
     * @var string
20
     */
21
    protected $appSecret;
22
23
    /**
24
     * @var string
25
     */
26
    protected $verifyToken;
27
28
    /**
29
     * @var string
30
     */
31
    protected $pageToken;
32
33
    /**
34
     * @var ClientInterface
35
     */
36
    protected $client;
37
38
    /**
39
     * Messenger constructor.
40
     *
41
     * @param string          $appSecret
42
     * @param string          $verifyToken
43
     * @param string          $pageToken
44
     * @param string          $apiVersion
45
     * @param ClientInterface $client
46
     */
47 1
    public function __construct(
48
        string $appSecret,
49
        string $verifyToken,
50
        string $pageToken,
51
        string $apiVersion = self::API_VERSION,
52
        ?ClientInterface $client = null
53
    ) {
54 1
        $this->appSecret = $appSecret;
55 1
        $this->verifyToken = $verifyToken;
56 1
        $this->pageToken = $pageToken;
57
58 1
        if ($client === null) {
59 1
            $client = $this->createClient($apiVersion);
60
        }
61 1
        $this->client = $client;
62 1
    }
63
64
    /**
65
     * @param string $apiVersion
66
     *
67
     * @return ClientInterface
68
     */
69 1
    private function createClient(string $apiVersion): ClientInterface
70
    {
71 1
        $stack = HandlerStack::create();
72 1
        $stack->push(Middleware::header('Content-Type', 'application/json'));
73 1
        $stack->push(Middleware::queryParam('access_token', $this->pageToken));
74
75 1
        return new Client([
76 1
            'handler' => $stack,
77 1
            'base_uri' => self::API_URL . $apiVersion . '/',
78
        ]);
79
    }
80
81
    /**
82
     * @return Api\Send
83
     */
84 1
    public function send(): Api\Send
85
    {
86 1
        return new Api\Send($this->client);
87
    }
88
89
    /**
90
     * @param ServerRequestInterface|null $request
91
     *
92
     * @return Api\Webhook
93
     */
94 1
    public function webhook(?ServerRequestInterface $request = null): Api\Webhook
95
    {
96 1
        return new Api\Webhook($this->appSecret, $this->verifyToken, $this->client, $request);
97
    }
98
99
    /**
100
     * @return Api\User
101
     */
102 1
    public function user(): Api\User
103
    {
104 1
        return new Api\User($this->client);
105
    }
106
107
    /**
108
     * @return Api\Profile
109
     */
110 1
    public function profile(): Api\Profile
111
    {
112 1
        return new Api\Profile($this->client);
113
    }
114
115
    /**
116
     * @return Api\Code
117
     */
118 1
    public function code(): Api\Code
119
    {
120 1
        return new Api\Code($this->client);
121
    }
122
123
    /**
124
     * @return Api\Insights
125
     */
126 1
    public function insights(): Api\Insights
127
    {
128 1
        return new Api\Insights($this->client);
129
    }
130
131
    /**
132
     * @return Api\Tag
133
     */
134 1
    public function tag(): Api\Tag
135
    {
136 1
        return new Api\Tag($this->client);
137
    }
138
139
    /**
140
     * @return Api\Thread
141
     */
142 1
    public function thread(): Api\Thread
143
    {
144 1
        return new Api\Thread($this->client);
145
    }
146
147
    /**
148
     * @return Api\Nlp
149
     */
150 1
    public function nlp(): Api\Nlp
151
    {
152 1
        return new Api\Nlp($this->client);
153
    }
154
155
    /**
156
     * @return Api\Broadcast
157
     */
158 1
    public function broadcast(): Api\Broadcast
159
    {
160 1
        return new Api\Broadcast($this->client);
161
    }
162
163
    /**
164
     * @return Api\Persona
165
     */
166 1
    public function persona(): Api\Persona
167
    {
168 1
        return new Api\Persona($this->client);
169
    }
170
}
171