Completed
Pull Request — master (#1)
by Romain
02:17
created

Thread   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 67
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 7 1
A delete() 0 9 1
A isValidThreadSettingType() 0 7 2
A getAllowedThreadSettingsType() 0 10 1
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\Client;
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
     * ThreadSettings constructor.
14
     *
15
     * @param string $pageToken
16
     * @param \GuzzleHttp\Client $client
17
     */
18
    public function __construct($pageToken, Client $client)
19
    {
20
        parent::__construct($pageToken, $client);
21
    }
22
23
    /**
24
     * @param \Kerox\Messenger\Model\ThreadSettings $threadSettings
25
     * @return \Kerox\Messenger\Response\ThreadResponse
26
     */
27
    public function add(ThreadSettings $threadSettings): ThreadResponse
28
    {
29
        $request = new ThreadRequest($this->pageToken, $threadSettings);
30
        $response = $this->client->post('/me/thread_settings', $request->build());
31
32
        return new ThreadResponse($response);
33
    }
34
35
    /**
36
     * @param string $type
37
     * @param string $state
38
     * @return void
39
     */
40
    public function delete(string $type, string $state = null)
41
    {
42
        $this->isValidThreadSettingType($type);
43
44
        $threadSettings = new ThreadSettings($type, $state);
45
46
        $request = new ThreadRequest($this->pageToken, $threadSettings);
47
        $this->client->delete('/me/thread_settings', $request->build());
48
    }
49
50
    /**
51
     * @param string $threadSettingsType
52
     * @throws \InvalidArgumentException
53
     */
54
    private function isValidThreadSettingType(string $threadSettingsType)
55
    {
56
        $allowedThreadSettingsType = $this->getAllowedThreadSettingsType();
57
        if (!in_array($threadSettingsType, $allowedThreadSettingsType)) {
58
            throw new \InvalidArgumentException('$threadSettingsType must be either ' . implode(', '), $allowedThreadSettingsType);
59
        }
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    private function getAllowedThreadSettingsType(): array
66
    {
67
        return [
68
            ThreadSettings::TYPE_GREETING,
69
            ThreadSettings::TYPE_CALL_TO_ACTIONS,
70
            ThreadSettings::TYPE_DOMAIN_WHITELISTING,
71
            ThreadSettings::TYPE_ACCOUNT_LINKING,
72
            ThreadSettings::TYPE_PAYMENT,
73
        ];
74
    }
75
}
76