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

Associate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 39
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A associationService() 0 4 1
A validate() 0 23 4
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\actions\objects;
10
11
use flipbox\craft\integration\actions\objects\Associate as AssociateIntegration;
12
use flipbox\craft\integration\records\IntegrationAssociation;
13
use flipbox\craft\integration\services\IntegrationAssociations;
14
use flipbox\hubspot\fields\Objects;
15
use flipbox\hubspot\HubSpot;
16
use flipbox\hubspot\services\ObjectAssociations;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class Associate extends AssociateIntegration
24
{
25
    /**
26
     * @inheritdoc
27
     * @return ObjectAssociations
28
     */
29
    protected function associationService(): IntegrationAssociations
30
    {
31
        return HubSpot::getInstance()->getObjectAssociations();
32
    }
33
34
    /**
35
     * @inheritdoc
36
     * @param IntegrationAssociation $record
37
     */
38
    protected function validate(
39
        IntegrationAssociation $record
40
    ): bool {
41
42
        if (null === ($fieldId = $record->fieldId)) {
43
            return false;
44
        }
45
46
        if (null === ($field = HubSpot::getInstance()->getObjectsField()->findById($fieldId))) {
47
            return false;
48
        }
49
50
        /** @var Objects $field */
51
52
        /** @var ResponseInterface $response */
53
        $response = $field->getResource()->rawHttpRead(
54
            $record->objectId,
55
            $field->getConnection(),
56
            $field->getCache()
57
        );
58
59
        return $response->getStatusCode() >= 200 && $response->getStatusCode() <= 299;
60
    }
61
}
62