Completed
Pull Request — master (#34)
by Romain
02:42
created

Thread::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\ClientInterface;
5
use Kerox\Messenger\Model\ThreadSettings;
6
use Kerox\Messenger\Request\ThreadRequest;
7
use Kerox\Messenger\Response\ThreadResponse;
8
9
class Thread extends AbstractApi
10
{
11
12
    /**
13
     * @var null|\Kerox\Messenger\Api\Thread
14
     */
15
    private static $_instance;
16
17
    /**
18
     * ThreadSettings constructor.
19
     *
20
     * @param string $pageToken
21
     * @param \GuzzleHttp\ClientInterface $client
22
     */
23 3
    public function __construct(string $pageToken, ClientInterface $client)
24
    {
25 3
        parent::__construct($pageToken, $client);
26 3
    }
27
28
    /**
29
     * @param string $pageToken
30
     * @param \GuzzleHttp\ClientInterface $client
31
     * @return \Kerox\Messenger\Api\Thread
32
     */
33 1
    public static function getInstance(string $pageToken, ClientInterface $client): Thread
34
    {
35 1
        if (self::$_instance === null) {
36 1
            self::$_instance = new Thread($pageToken, $client);
37
        }
38
39 1
        return self::$_instance;
40
    }
41
42
    /**
43
     * @param \Kerox\Messenger\Model\ThreadSettings $threadSettings
44
     * @return \Kerox\Messenger\Response\ThreadResponse
45
     */
46 1
    public function addSetting(ThreadSettings $threadSettings): ThreadResponse
47
    {
48 1
        $request = new ThreadRequest($this->pageToken, $threadSettings);
49 1
        $response = $this->client->post('me/thread_settings', $request->build());
50
51 1
        return new ThreadResponse($response);
52
    }
53
54
    /**
55
     * @param string $type
56
     * @param string $state
57
     * @return void
58
     */
59 1
    public function deleteSetting(string $type, string $state = null)
60
    {
61 1
        $this->isValidThreadSettingType($type);
62
63 1
        $threadSettings = new ThreadSettings($type, $state);
64
65
        $request = new ThreadRequest($this->pageToken, $threadSettings);
66
        $this->client->delete('me/thread_settings', $request->build());
67
    }
68
69
    /**
70
     * @param string $threadSettingsType
71
     * @throws \InvalidArgumentException
72
     */
73 1
    private function isValidThreadSettingType(string $threadSettingsType)
74
    {
75 1
        $allowedThreadSettingsType = $this->getAllowedThreadSettingsType();
76 1
        if (!in_array($threadSettingsType, $allowedThreadSettingsType)) {
77
            throw new \InvalidArgumentException('$threadSettingsType must be either ' . implode(', '), $allowedThreadSettingsType);
78
        }
79 1
    }
80
81
    /**
82
     * @return array
83
     */
84 1
    private function getAllowedThreadSettingsType(): array
85
    {
86
        return [
87 1
            ThreadSettings::TYPE_GREETING,
88 1
            ThreadSettings::TYPE_CALL_TO_ACTIONS,
89 1
            ThreadSettings::TYPE_DOMAIN_WHITELISTING,
90 1
            ThreadSettings::TYPE_ACCOUNT_LINKING,
91 1
            ThreadSettings::TYPE_PAYMENT,
92
        ];
93
    }
94
}
95