Completed
Push — develop ( 1d947e...a9d8b7 )
by Nate
05:38
created

HubSpotIdTrait::findHubSpotId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 12
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\services\resources\traits;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\fields\Resources;
14
use flipbox\hubspot\HubSpot;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait HubSpotIdTrait
21
{
22
    /**
23
     * @param ElementInterface $element
24
     * @param Resources $field
25
     * @return string|null
26
     */
27
    public function findHubSpotIdByElementAssociation(ElementInterface $element, Resources $field)
28
    {
29
        /** @var Element $element */
30
        return $this->findHubSpotId($field->id, $element->getId(), $element->siteId);
31
    }
32
33
    /**
34
     * @noinspection PhpDocMissingThrowsInspection
35
     *
36
     * @param string $fieldId
37
     * @param string $elementId
38
     * @param string|null $siteId
39
     * @return null|string
40
     */
41
    public function findHubSpotId(string $fieldId, string $elementId, string $siteId = null)
42
    {
43
        if ($siteId === null) {
44
            /** @noinspection PhpUnhandledExceptionInspection */
45
            $siteId = \Craft::$app->getSites()->getCurrentSite()->id;
46
        }
47
48
        $hubSpotId = HubSpot::getInstance()->getResourceAssociations()->getQuery([
49
            'select' => ['hubSpotId'],
50
            'elementId' => $elementId,
51
            'siteId' => $siteId,
52
            'fieldId' => $fieldId
53
        ])->scalar();
54
55
        return is_string($hubSpotId) ? $hubSpotId : null;
56
    }
57
}
58