Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

Companies::displayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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;
12
use craft\helpers\Json;
13
use flipbox\craft\hubspot\criteria\CompanyCriteria;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class Companies extends Objects
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function getObjectLabel(): string
26
    {
27
        return 'Company';
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public static function displayName(): string
34
    {
35
        return Craft::t('hubspot', 'HubSpot: Companies');
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public static function defaultSelectionLabel(): string
42
    {
43
        return Craft::t('hubspot', 'Add a HubSpot Company');
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 CompanyCriteria([
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 CompanyCriteria([
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['companyId'] ?? ($data['vid'] ?? null);
88
89
        return $id ? (string)$id : null;
90
    }
91
}
92