Completed
Push — master ( d56c86...719266 )
by Nate
06:29 queued 04:50
created

ObjectId::transformerElementToId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\hubspot\transformers\elements;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\ember\helpers\SiteHelper;
14
use flipbox\hubspot\fields\Objects;
15
use flipbox\hubspot\HubSpot;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ObjectId
22
{
23
    /**
24
     * @var Objects
25
     */
26
    protected $field;
27
28
    /**
29
     * @param Objects $field
30
     * @inheritdoc
31
     */
32
    public function __construct(Objects $field)
33
    {
34
        $this->field = $field;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     * @param Element $data
40
     * @return string|null
41
     */
42
    public function __invoke(ElementInterface $data)
43
    {
44
        $objectId = HubSpot::getInstance()->getObjectAssociations()->getQuery([
45
            'select' => ['objectId'],
46
            'elementId' => $data->getId(),
47
            'siteId' => SiteHelper::ensureSiteId($data->siteId),
48
            'fieldId' => $this->field->id
49
        ])->scalar();
50
51
        if (!is_string($objectId)) {
52
            HubSpot::warning(sprintf(
53
                "HubSpot Id association was not found for element '%s'",
54
                $data->getId()
55
            ));
56
57
            return null;
58
        }
59
60
        HubSpot::info(sprintf(
61
            "HubSpot Id '%s' was found for element '%s'",
62
            $objectId,
63
            $data->getId()
64
        ));
65
66
        return $objectId;
67
    }
68
}
69