Completed
Pull Request — master (#29)
by Romain
02:25
created

Messenger   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 134
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

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