Completed
Push — develop ( f8c4bd...63ca71 )
by Nate
02:47
created

ObjectAssociation::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace flipbox\hubspot\records;
4
5
use Craft;
6
use flipbox\craft\sortable\associations\records\SortableAssociation;
7
use flipbox\craft\sortable\associations\services\SortableAssociations;
8
use flipbox\ember\helpers\ModelHelper;
9
use flipbox\ember\records\traits\ElementAttribute;
10
use flipbox\ember\records\traits\SiteAttribute;
11
use flipbox\hubspot\db\ObjectAssociationQuery;
12
use flipbox\hubspot\fields\Objects;
13
use flipbox\hubspot\HubSpot;
14
use flipbox\hubspot\services\ObjectAssociations;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 *
20
 * @property int $fieldId
21
 * @property string $objectId
22
 */
23
class ObjectAssociation extends SortableAssociation
24
{
25
    use SiteAttribute,
26
        ElementAttribute,
27
        traits\FieldAttribute;
28
29
    /**
30
     * The table alias
31
     */
32
    const TABLE_ALIAS = 'hubspot_objects';
33
34
    /**
35
     * @inheritdoc
36
     */
37
    const TARGET_ATTRIBUTE = 'objectId';
38
39
    /**
40
     * @inheritdoc
41
     */
42
    const SOURCE_ATTRIBUTE = 'elementId';
43
44
    /**
45
     * @noinspection PhpDocMissingThrowsInspection
46
     * @return ObjectAssociationQuery
47
     */
48
    public static function find(): ObjectAssociationQuery
49
    {
50
        /** @noinspection PhpIncompatibleReturnTypeInspection */
51
        /** @noinspection PhpUnhandledExceptionInspection */
52
        return Craft::createObject(ObjectAssociationQuery::class, [get_called_class()]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public static function tableAlias()
59
    {
60
        return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @return ObjectAssociations
66
     */
67
    protected function associationService(): SortableAssociations
68
    {
69
        return HubSpot::getInstance()->getObjectAssociations();
70
    }
71
72
    /**
73
     * @param array $criteria
74
     * @return mixed|null
75
     * @throws \yii\base\InvalidConfigException
76
     */
77
    public function getResource(array $criteria = [])
78
    {
79
        if (null === ($field = $this->getField())) {
80
            return null;
81
        }
82
83
        if (!$field instanceof Objects) {
84
            return null;
85
        }
86
87
        return $field->createResourceCriteria($this, $criteria)->fetch();
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function rules(): array
94
    {
95
        return array_merge(
96
            parent::rules(),
97
            $this->siteRules(),
98
            $this->elementRules(),
99
            $this->fieldRules(),
100
            [
101
                [
102
                    [
103
                        self::TARGET_ATTRIBUTE,
104
                    ],
105
                    'required'
106
                ],
107
                [
108
                    self::TARGET_ATTRIBUTE,
109
                    'unique',
110
                    'targetAttribute' => [
111
                        'elementId',
112
                        'fieldId',
113
                        'siteId',
114
                        self::TARGET_ATTRIBUTE
115
                    ]
116
                ],
117
                [
118
                    [
119
                        self::TARGET_ATTRIBUTE
120
                    ],
121
                    'safe',
122
                    'on' => [
123
                        ModelHelper::SCENARIO_DEFAULT
124
                    ]
125
                ]
126
            ]
127
        );
128
    }
129
}
130