Completed
Push — master ( 719266...e4b221 )
by Nate
06:18 queued 04:34
created

ObjectAssociation::getResource()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 15
cp 0
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 1
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\hubspot\records;
10
11
use Craft;
12
use flipbox\craft\integration\records\IntegrationAssociation;
13
use flipbox\craft\sortable\associations\services\SortableAssociations;
14
use flipbox\hubspot\db\ObjectAssociationQuery;
15
use flipbox\hubspot\fields\Objects;
16
use flipbox\hubspot\HubSpot;
17
use flipbox\hubspot\services\ObjectAssociations;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @property int $fieldId
24
 * @property string $objectId
25
 */
26
class ObjectAssociation extends IntegrationAssociation
27
{
28
    /**
29
     * The table alias
30
     */
31
    const TABLE_ALIAS = 'hubspot_objects';
32
33
    /**
34
     * @inheritdoc
35
     * @throws \Throwable
36
     */
37
    public function __construct($config = [])
38
    {
39
        HubSpot::getInstance()->getObjectAssociations()->ensureTableExists();
40
        parent::__construct($config);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public static function tableAlias()
47
    {
48 3
        return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     * @return ObjectAssociations
54
     */
55
    protected function associationService(): SortableAssociations
56
    {
57
        return HubSpot::getInstance()->getObjectAssociations();
58
    }
59
60
    /**
61
     * @noinspection PhpDocMissingThrowsInspection
62
     * @return ObjectAssociationQuery
63
     */
64
    public static function find(): ObjectAssociationQuery
65
    {
66
        /** @noinspection PhpIncompatibleReturnTypeInspection */
67
        /** @noinspection PhpUnhandledExceptionInspection */
68
        return Craft::createObject(ObjectAssociationQuery::class, [get_called_class()]);
69
    }
70
71
    /**
72
     * @param array $criteria
73
     * @return mixed|null
74
     * @throws \yii\base\InvalidConfigException
75
     */
76
    public function getObject(array $criteria = [])
77
    {
78
        if (null === ($field = $this->getField())) {
79
            return null;
80
        }
81
82
        if (!$field instanceof Objects) {
83
            return null;
84
        }
85
86
        $base = [
87
            'connection' => $field->getConnection(),
88
            'cache' => $field->getCache()
89
        ];
90
91
        $resource = $field->getResource();
92
93
        // Can't override these...
94
        $criteria['id'] = $this->{self::TARGET_ATTRIBUTE} ?: self::DEFAULT_ID;
95
        $criteria['object'] = $field->object;
96
97
        return $resource->read(
98
            $resource->getAccessorCriteria(
99
                array_merge(
100
                    $base,
101
                    $criteria
102
                )
103
            )
104
        );
105
    }
106
}
107