Passed
Push — master ( f115d8...f636e9 )
by Romain
04:41
created

Thread::deleteSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 6
cp 0.5
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1.125
1
<?php
2
3
namespace Kerox\Messenger\Api;
4
5
use GuzzleHttp\ClientInterface;
6
use Kerox\Messenger\Model\ThreadSettings;
7
use Kerox\Messenger\Request\ThreadRequest;
8
use Kerox\Messenger\Response\ThreadResponse;
9
10
/**
11
 * @deprecated since 1.2.0 and will be remove in 1.3.0.
12
 * @see \Kerox\Messenger\Api\Profile
13
 */
14
class Thread extends AbstractApi
15
{
16
17
    /**
18
     * @var null|\Kerox\Messenger\Api\Thread
19
     */
20
    private static $_instance;
21
22
    /**
23
     * ThreadSettings constructor.
24
     *
25
     * @param string $pageToken
26
     * @param \GuzzleHttp\ClientInterface $client
27
     */
28 3
    public function __construct(string $pageToken, ClientInterface $client)
29
    {
30 3
        parent::__construct($pageToken, $client);
31 3
    }
32
33
    /**
34
     * @param string $pageToken
35
     * @param \GuzzleHttp\ClientInterface $client
36
     * @return \Kerox\Messenger\Api\Thread
37
     */
38 1
    public static function getInstance(string $pageToken, ClientInterface $client): Thread
39
    {
40 1
        if (self::$_instance === null) {
41 1
            self::$_instance = new Thread($pageToken, $client);
0 ignored issues
show
Deprecated Code introduced by
The class Kerox\Messenger\Api\Thread has been deprecated with message: since 1.2.0 and will be remove in 1.3.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
42
        }
43
44 1
        return self::$_instance;
45
    }
46
47
    /**
48
     * @param \Kerox\Messenger\Model\ThreadSettings $threadSettings
49
     * @return \Kerox\Messenger\Response\ThreadResponse
50
     */
51 1
    public function addSetting(ThreadSettings $threadSettings): ThreadResponse
52
    {
53 1
        $request = new ThreadRequest($this->pageToken, $threadSettings);
54 1
        $response = $this->client->post('me/thread_settings', $request->build());
55
56 1
        return new ThreadResponse($response);
57
    }
58
59
    /**
60
     * @param string $type
61
     * @param string $state
62
     * @return void
63
     */
64 1
    public function deleteSetting(string $type, string $state = null)
65
    {
66 1
        $this->isValidThreadSettingType($type);
67
68 1
        $threadSettings = new ThreadSettings($type, $state);
0 ignored issues
show
Deprecated Code introduced by
The class Kerox\Messenger\Model\ThreadSettings has been deprecated with message: since 1.2.0 and will be remove in 1.3.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
69
70
        $request = new ThreadRequest($this->pageToken, $threadSettings);
71
        $this->client->delete('me/thread_settings', $request->build());
72
    }
73
74
    /**
75
     * @param string $threadSettingsType
76
     * @throws \InvalidArgumentException
77
     */
78 1
    private function isValidThreadSettingType(string $threadSettingsType)
79
    {
80 1
        $allowedThreadSettingsType = $this->getAllowedThreadSettingsType();
81 1
        if (!in_array($threadSettingsType, $allowedThreadSettingsType)) {
82
            throw new \InvalidArgumentException('$threadSettingsType must be either ' . implode(', '), $allowedThreadSettingsType);
83
        }
84 1
    }
85
86
    /**
87
     * @return array
88
     */
89 1
    private function getAllowedThreadSettingsType(): array
90
    {
91
        return [
92 1
            ThreadSettings::TYPE_GREETING,
93 1
            ThreadSettings::TYPE_CALL_TO_ACTIONS,
94 1
            ThreadSettings::TYPE_DOMAIN_WHITELISTING,
95 1
            ThreadSettings::TYPE_ACCOUNT_LINKING,
96 1
            ThreadSettings::TYPE_PAYMENT,
97
        ];
98
    }
99
}
100