Completed
Push — develop ( 136153...525928 )
by Nate
03:34
created

ObjectAssociation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 5.56%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 8
dl 0
loc 123
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 3
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
     * @inheritdoc
52
     * @throws \Throwable
53
     */
54
    public function __construct($config = [])
55
    {
56
        HubSpot::getInstance()->getObjectAssociations()->ensureTableExists();
57
        parent::__construct($config);
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
     * {@inheritdoc}
73
     */
74 3
    public static function tableAlias()
75
    {
76 3
        return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     * @return ObjectAssociations
82
     */
83
    protected function associationService(): SortableAssociations
84
    {
85
        return HubSpot::getInstance()->getObjectAssociations();
86
    }
87
88
    /**
89
     * @param array $criteria
90
     * @return mixed|null
91
     * @throws \yii\base\InvalidConfigException
92
     */
93
    public function getResource(array $criteria = [])
0 ignored issues
show
Unused Code introduced by
The parameter $criteria is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        if (null === ($field = $this->getField())) {
96
            return null;
97
        }
98
99
        if (!$field instanceof Objects) {
100
            return null;
101
        }
102
103
        $resource = $field->getResource();
104
105
        $criteria = $resource->getCriteria([
106
            'id' => $this->objectId
107
        ]);
108
109
        return $resource->read($criteria);
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function rules(): array
116
    {
117
        return array_merge(
118
            parent::rules(),
119
            $this->siteRules(),
120
            $this->elementRules(),
121
            $this->fieldRules(),
122
            [
123
                [
124
                    [
125
                        self::TARGET_ATTRIBUTE,
126
                    ],
127
                    'required'
128
                ],
129
                [
130
                    self::TARGET_ATTRIBUTE,
131
                    'unique',
132
                    'targetAttribute' => [
133
                        'elementId',
134
                        'fieldId',
135
                        'siteId',
136
                        self::TARGET_ATTRIBUTE
137
                    ]
138
                ],
139
                [
140
                    [
141
                        self::TARGET_ATTRIBUTE
142
                    ],
143
                    'safe',
144
                    'on' => [
145
                        ModelHelper::SCENARIO_DEFAULT
146
                    ]
147
                ]
148
            ]
149
        );
150
    }
151
}
152