SyncFrom::autoResolveElement()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 28
cp 0
rs 9.392
c 0
b 0
f 0
cc 4
nc 6
nop 4
crap 20
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\cp\actions\widgets;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\ember\helpers\SiteHelper;
14
use flipbox\craft\hubspot\cp\actions\sync\AbstractSyncFrom;
15
use flipbox\craft\hubspot\fields\Objects;
16
use flipbox\craft\hubspot\HubSpot;
17
use flipbox\craft\hubspot\records\ObjectAssociation;
18
use flipbox\craft\integration\actions\ResolverTrait;
19
use flipbox\craft\integration\fields\Integrations;
20
use flipbox\craft\integration\queries\IntegrationAssociationQuery;
21
use yii\web\HttpException;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class SyncFrom extends AbstractSyncFrom
28
{
29
    use ResolverTrait;
30
31
    /**
32
     * @param string $id
33
     * @param string $field
34
     * @param string $elementType
35
     * @param int|null $siteId
36
     * @return ElementInterface|mixed
37
     * @throws HttpException
38
     * @throws \yii\web\UnauthorizedHttpException
39
     */
40
    public function run(string $id, string $field, string $elementType, int $siteId = null)
41
    {
42
        /** @var Objects $field */
43
        $field = $this->resolveField($field);
44
45
        /** @var ElementInterface $element */
46
        $element = $this->autoResolveElement($field, $id, $elementType, $siteId);
47
48
        return $this->runInternal($element, $field, $id);
49
    }
50
51
    /**
52
     * @param Integrations $field
53
     * @param string $id
54
     * @param string $elementType
55
     * @param int|null $siteId
56
     * @return ElementInterface
57
     */
58
    private function autoResolveElement(
59
        Integrations $field,
60
        string $id,
61
        string $elementType,
62
        int $siteId = null
63
    ): ElementInterface {
64
65
        /** @var IntegrationAssociationQuery $query */
66
        $query = ObjectAssociation::find()
67
            ->select(['elementId'])
68
            ->fieldId($field->id)
69
            ->objectId($id)
70
            ->siteId(SiteHelper::ensureSiteId($siteId));
71
72
        if ($elementId = $query->scalar()) {
73
            try {
74
                $element = $this->resolveElement($elementId);
75
            } catch (HttpException $e) {
76
                HubSpot::warning(sprintf(
77
                    "Unable to find element '%s' associated to HubSpot field '%s' Id '%s'",
78
                    $elementId,
79
                    $field->handle,
80
                    $id
81
                ));
82
            }
83
        }
84
85
        if (empty($element)) {
86
            $element = Craft::$app->getElements()->createElement($elementType);
87
        }
88
89
        return $element;
90
    }
91
}
92