Completed
Push — master ( c30a0b...37c0e6 )
by Nate
08:20
created

Contacts::getObjectLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot\fields;
10
11
use craft\helpers\Json;
12
use flipbox\craft\hubspot\criteria\ContactCriteria;
13
use flipbox\craft\hubspot\HubSpot;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class Contacts extends Objects
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function getObjectLabel(): string
26
    {
27
        return 'Contact';
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public static function displayName(): string
34
    {
35
        return HubSpot::t('HubSpot: Contacts');
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public static function defaultSelectionLabel(): string
42
    {
43
        return HubSpot::t('Add a HubSpot Contact');
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @throws \flipbox\craft\integration\exceptions\ConnectionNotFound
49
     */
50
    protected function upsertToHubSpot(
51
        array $payload,
52
        string $id = null
53
    ): ResponseInterface {
54
        return (new ContactCriteria([
55
            'connection' => $this->getConnection(),
56
            'cache' => $this->getCache(),
57
            'payload' => $payload,
58
            'id' => $id
59
        ]))->upsert();
60
    }
61
62
    /**
63
     * @inheritdoc
64
     * @throws \flipbox\craft\integration\exceptions\ConnectionNotFound
65
     * @throws \Exception
66
     */
67
    public function readFromHubSpot(
68
        string $id
69
    ): ResponseInterface {
70
        return (new ContactCriteria([
71
            'connection' => $this->getConnection(),
72
            'cache' => $this->getCache(),
73
            'id' => $id
74
        ]))->read();
75
    }
76
77
    /**
78
     * @param ResponseInterface $response
79
     * @return string|null
80
     */
81
    protected function getObjectIdFromResponse(ResponseInterface $response)
82
    {
83
        $data = Json::decodeIfJson(
84
            $response->getBody()->getContents()
85
        );
86
87
        $id = $data['vid'] ?? null;
88
89
        return $id ? (string)$id : null;
90
    }
91
}
92