Completed
Pull Request — master (#30)
by Romain
02:57 queued 31s
created

Messenger   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 148
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A send() 0 8 2
A webhook() 0 8 2
A user() 0 8 2
A thread() 0 8 2
A code() 0 8 2
A insights() 0 8 2
1
<?php
2
namespace Kerox\Messenger;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
6
use Kerox\Messenger\Api\Code;
7
use Kerox\Messenger\Api\Insights;
8
use Kerox\Messenger\Api\Send;
9
use Kerox\Messenger\Api\Thread;
10
use Kerox\Messenger\Api\User;
11
use Kerox\Messenger\Api\Webhook;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
class Messenger
15
{
16
17
    const API_URL = 'https://graph.facebook.com/';
18
    const API_VERSION = 'v2.8';
19
20
    /**
21
     * @var string
22
     */
23
    protected $appSecret;
24
25
    /**
26
     * @var string
27
     */
28
    protected $verifyToken;
29
30
    /**
31
     * @var string
32
     */
33
    protected $pageToken;
34
35
    /**
36
     * @var \GuzzleHttp\ClientInterface
37
     */
38
    protected $client;
39
40
    /**
41
     * @var \Kerox\Messenger\Api\Send
42
     */
43
    protected $sendApi;
44
45
    /**
46
     * @var \Kerox\Messenger\Api\Webhook
47
     */
48
    protected $webhookApi;
49
50
    /**
51
     * @var \Kerox\Messenger\Api\User
52
     */
53
    protected $userApi;
54
55
    /**
56
     * @var \Kerox\Messenger\Api\Thread
57
     */
58
    protected $threadApi;
59
60
    /**
61
     * @var \Kerox\Messenger\Api\Code
62
     */
63
    protected $codeApi;
64
65
    /**
66
     * @var \Kerox\Messenger\Api\Insights
67
     */
68
    protected $insightsApi;
69
70
    /**
71
     * Messenger constructor.
72
     *
73
     * @param string $appSecret
74
     * @param string $verifyToken
75
     * @param string $pageToken
76
     * @param \GuzzleHttp\ClientInterface $client
77
     */
78 1
    public function __construct(string $appSecret, string $verifyToken, string $pageToken, ClientInterface $client = null)
79
    {
80 1
        $this->appSecret = $appSecret;
81 1
        $this->verifyToken = $verifyToken;
82 1
        $this->pageToken = $pageToken;
83
84 1
        if ($client === null) {
85 1
            $client = new Client([
86 1
                'base_uri' => self::API_URL . self::API_VERSION . '/',
87
            ]);
88
        }
89 1
        $this->client = $client;
90 1
    }
91
92
    /**
93
     * @return \Kerox\Messenger\Api\Send
94
     */
95 1
    public function send(): Send
96
    {
97 1
        if ($this->sendApi === null) {
98 1
            $this->sendApi = new Send($this->pageToken, $this->client);
99
        }
100
101 1
        return $this->sendApi;
102
    }
103
104
    /**
105
     * @param \Psr\Http\Message\ServerRequestInterface $request
106
     * @return \Kerox\Messenger\Api\Webhook
107
     */
108 1
    public function webhook(ServerRequestInterface $request = null): Webhook
109
    {
110 1
        if ($this->webhookApi === null) {
111 1
            $this->webhookApi = new Webhook($this->appSecret, $this->verifyToken, $this->pageToken, $this->client, $request);
112
        }
113
114 1
        return $this->webhookApi;
115
    }
116
117
    /**
118
     * @return \Kerox\Messenger\Api\User
119
     */
120 1
    public function user(): User
121
    {
122 1
        if ($this->userApi === null) {
123 1
            $this->userApi = new User($this->pageToken, $this->client);
124
        }
125
126 1
        return $this->userApi;
127
    }
128
129
    /**
130
     * @return \Kerox\Messenger\Api\Thread
131
     */
132 1
    public function thread(): Thread
133
    {
134 1
        if ($this->threadApi === null) {
135 1
            $this->threadApi = new Thread($this->pageToken, $this->client);
136
        }
137
138 1
        return $this->threadApi;
139
    }
140
141
    /**
142
     * @return \Kerox\Messenger\Api\Code
143
     */
144 1
    public function code(): Code
145
    {
146 1
        if ($this->codeApi === null) {
147 1
            $this->codeApi = new Code($this->pageToken, $this->client);
148
        }
149
150 1
        return $this->codeApi;
151
    }
152
153 1
    public function insights(): Insights
154
    {
155 1
        if ($this->insightsApi === null) {
156 1
            $this->insightsApi = new Insights($this->pageToken, $this->client);
157
        }
158
159 1
        return $this->insightsApi;
160
    }
161
}
162