Completed
Push — master ( 136153...d5a3eb )
by Nate
19:29 queued 17:38
created

ObjectAssociation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 5.56%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 8
dl 0
loc 128
ccs 2
cts 36
cp 0.0556
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 6 1
A tableAlias() 0 4 1
A associationService() 0 4 1
A getResource() 0 18 4
A rules() 0 36 1
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\sortable\associations\records\SortableAssociation;
13
use flipbox\craft\sortable\associations\services\SortableAssociations;
14
use flipbox\ember\helpers\ModelHelper;
15
use flipbox\ember\records\traits\ElementAttribute;
16
use flipbox\ember\records\traits\SiteAttribute;
17
use flipbox\hubspot\db\ObjectAssociationQuery;
18
use flipbox\hubspot\fields\Objects;
19
use flipbox\hubspot\HubSpot;
20
use flipbox\hubspot\services\ObjectAssociations;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @property int $fieldId
27
 * @property string $objectId
28
 */
29
class ObjectAssociation extends SortableAssociation
30
{
31
    use SiteAttribute,
32
        ElementAttribute,
33
        traits\FieldAttribute;
34
35
    /**
36
     * The table alias
37
     */
38
    const TABLE_ALIAS = 'hubspot_objects';
39
40
    /**
41
     * @inheritdoc
42
     */
43
    const TARGET_ATTRIBUTE = 'objectId';
44
45
    /**
46
     * @inheritdoc
47
     */
48
    const SOURCE_ATTRIBUTE = 'elementId';
49
50
    /**
51
     * The default HubSpot Resource Id (if none exists)
52
     */
53
    const DEFAULT_HUBSPOT_ID = 'UNKNOWN_ID';
54
55
    /**
56
     * @inheritdoc
57
     * @throws \Throwable
58
     */
59
    public function __construct($config = [])
60
    {
61
        HubSpot::getInstance()->getObjectAssociations()->ensureTableExists();
62
        parent::__construct($config);
63
    }
64
65
    /**
66
     * @noinspection PhpDocMissingThrowsInspection
67
     * @return ObjectAssociationQuery
68
     */
69
    public static function find(): ObjectAssociationQuery
70
    {
71
        /** @noinspection PhpIncompatibleReturnTypeInspection */
72
        /** @noinspection PhpUnhandledExceptionInspection */
73
        return Craft::createObject(ObjectAssociationQuery::class, [get_called_class()]);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 3
    public static function tableAlias()
80
    {
81 3
        return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     * @return ObjectAssociations
87
     */
88
    protected function associationService(): SortableAssociations
89
    {
90
        return HubSpot::getInstance()->getObjectAssociations();
91
    }
92
93
    /**
94
     * @param array $criteria
95
     * @return mixed|null
96
     * @throws \yii\base\InvalidConfigException
97
     */
98
    public function getResource(array $criteria = [])
99
    {
100
        if (null === ($field = $this->getField())) {
101
            return null;
102
        }
103
104
        if (!$field instanceof Objects) {
105
            return null;
106
        }
107
108
        $resource = $field->getResource();
109
110
        $criteria['id'] = $this->objectId ?: self::DEFAULT_HUBSPOT_ID;
111
112
        return $resource->read(
113
            $resource->getCriteria($criteria)
114
        );
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function rules(): array
121
    {
122
        return array_merge(
123
            parent::rules(),
124
            $this->siteRules(),
125
            $this->elementRules(),
126
            $this->fieldRules(),
127
            [
128
                [
129
                    [
130
                        self::TARGET_ATTRIBUTE,
131
                    ],
132
                    'required'
133
                ],
134
                [
135
                    self::TARGET_ATTRIBUTE,
136
                    'unique',
137
                    'targetAttribute' => [
138
                        'elementId',
139
                        'fieldId',
140
                        'siteId',
141
                        self::TARGET_ATTRIBUTE
142
                    ]
143
                ],
144
                [
145
                    [
146
                        self::TARGET_ATTRIBUTE
147
                    ],
148
                    'safe',
149
                    'on' => [
150
                        ModelHelper::SCENARIO_DEFAULT
151
                    ]
152
                ]
153
            ]
154
        );
155
    }
156
}
157