1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Api; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidKeyException; |
8
|
|
|
use Kerox\Messenger\Model\ProfileSettings; |
9
|
|
|
use Kerox\Messenger\ProfileInterface; |
10
|
|
|
use Kerox\Messenger\Request\ProfileRequest; |
11
|
|
|
use Kerox\Messenger\Response\ProfileResponse; |
12
|
|
|
|
13
|
|
|
class Profile extends AbstractApi implements ProfileInterface |
14
|
|
|
{ |
15
|
1 |
|
public function add(ProfileSettings $profileSettings): ProfileResponse |
16
|
|
|
{ |
17
|
1 |
|
$request = new ProfileRequest($this->pageToken, $profileSettings); |
18
|
1 |
|
$response = $this->client->post('me/messenger_profile', $request->build()); |
19
|
|
|
|
20
|
1 |
|
return new ProfileResponse($response); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
25
|
|
|
*/ |
26
|
1 |
View Code Duplication |
public function get(array $profileSettings): ProfileResponse |
|
|
|
|
27
|
|
|
{ |
28
|
1 |
|
$this->isValidFields($profileSettings); |
29
|
|
|
|
30
|
1 |
|
$profileSettings = implode(',', $profileSettings); |
31
|
|
|
|
32
|
1 |
|
$request = new ProfileRequest($this->pageToken, $profileSettings); |
33
|
1 |
|
$response = $this->client->get('me/messenger_profile', $request->build()); |
34
|
|
|
|
35
|
1 |
|
return new ProfileResponse($response); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
40
|
|
|
*/ |
41
|
2 |
View Code Duplication |
public function delete(array $profileSettings): ProfileResponse |
|
|
|
|
42
|
|
|
{ |
43
|
2 |
|
$this->isValidFields($profileSettings); |
44
|
|
|
|
45
|
1 |
|
$request = new ProfileRequest($this->pageToken, $profileSettings); |
46
|
1 |
|
$response = $this->client->delete('me/messenger_profile', $request->build()); |
47
|
|
|
|
48
|
1 |
|
return new ProfileResponse($response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
53
|
|
|
*/ |
54
|
3 |
|
private function isValidFields(array $fields): void |
55
|
|
|
{ |
56
|
3 |
|
$allowedFields = $this->getAllowedFields(); |
57
|
3 |
View Code Duplication |
foreach ($fields as $field) { |
|
|
|
|
58
|
3 |
|
if (!\in_array($field, $allowedFields, true)) { |
59
|
1 |
|
throw new InvalidKeyException(sprintf('%s is not a valid value. fields must only contain "%s".', $field, implode(', ', $allowedFields))); |
60
|
|
|
} |
61
|
|
|
} |
62
|
2 |
|
} |
63
|
|
|
|
64
|
3 |
|
private function getAllowedFields(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
3 |
|
self::GET_STARTED, |
68
|
3 |
|
self::GREETING, |
69
|
3 |
|
self::ICE_BREAKERS, |
70
|
3 |
|
self::PERSISTENT_MENU, |
71
|
3 |
|
self::DOMAIN_WHITELISTING, |
72
|
3 |
|
self::ACCOUNT_LINKING_URL, |
73
|
3 |
|
self::PAYMENT_SETTINGS, |
74
|
3 |
|
self::HOME_URL, |
75
|
3 |
|
self::TARGET_AUDIENCE, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.