Profile::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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