Completed
Push — master ( 874936...5f953b )
by Nate
10:31 queued 07:47
created

ObjectAssociation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
     * @inheritdoc
52
     */
53
    public function __construct($config = [])
54
    {
55
        HubSpot::getInstance()->getObjectAssociations()->ensureTableExists();
56
        parent::__construct($config);
57
    }
58
59
    /**
60
     * @noinspection PhpDocMissingThrowsInspection
61
     * @return ObjectAssociationQuery
62
     */
63
    public static function find(): ObjectAssociationQuery
64
    {
65
        /** @noinspection PhpIncompatibleReturnTypeInspection */
66
        /** @noinspection PhpUnhandledExceptionInspection */
67
        return Craft::createObject(ObjectAssociationQuery::class, [get_called_class()]);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 3
    public static function tableAlias()
74
    {
75 3
        return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     * @return ObjectAssociations
81
     */
82
    protected function associationService(): SortableAssociations
83
    {
84
        return HubSpot::getInstance()->getObjectAssociations();
85
    }
86
87
    /**
88
     * @param array $criteria
89
     * @return mixed|null
90
     * @throws \yii\base\InvalidConfigException
91
     */
92
    public function getResource(array $criteria = [])
93
    {
94
        if (null === ($field = $this->getField())) {
95
            return null;
96
        }
97
98
        if (!$field instanceof Objects) {
99
            return null;
100
        }
101
102
        return $field->createResourceCriteria($this, $criteria)->fetch();
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function rules(): array
109
    {
110
        return array_merge(
111
            parent::rules(),
112
            $this->siteRules(),
113
            $this->elementRules(),
114
            $this->fieldRules(),
115
            [
116
                [
117
                    [
118
                        self::TARGET_ATTRIBUTE,
119
                    ],
120
                    'required'
121
                ],
122
                [
123
                    self::TARGET_ATTRIBUTE,
124
                    'unique',
125
                    'targetAttribute' => [
126
                        'elementId',
127
                        'fieldId',
128
                        'siteId',
129
                        self::TARGET_ATTRIBUTE
130
                    ]
131
                ],
132
                [
133
                    [
134
                        self::TARGET_ATTRIBUTE
135
                    ],
136
                    'safe',
137
                    'on' => [
138
                        ModelHelper::SCENARIO_DEFAULT
139
                    ]
140
                ]
141
            ]
142
        );
143
    }
144
}
145