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

Thread   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 86
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 8 2
A addSetting() 0 7 1
A getAllowedThreadSettingsType() 0 10 1
A deleteSetting() 0 9 1
A isValidThreadSettingType() 0 7 2
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