Completed
Push — master ( 3407e1...79fe03 )
by Nate
07:26
created

IdAttributeFromElementTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 55
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getFieldId() 0 1 ?
getElementId() 0 1 ?
getSiteId() 0 1 ?
A findId() 0 8 2
A resolveId() 0 20 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\craft\salesforce\criteria;
10
11
use flipbox\craft\ember\helpers\SiteHelper;
12
use flipbox\craft\salesforce\records\ObjectAssociation;
13
use Flipbox\HubSpot\Criteria\IdAttributeTrait;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.1.0
18
 */
19
trait IdAttributeFromElementTrait
20
{
21
    use IdAttributeTrait;
22
23
    /**
24
     * @return int|null
25
     */
26
    abstract public function getFieldId();
27
28
    /**
29
     * @return int|null
30
     */
31
    abstract public function getElementId();
32
33
    /**
34
     * @return int|null
35
     */
36
    abstract public function getSiteId();
37
38
    /**
39
     * @return string|null
40
     */
41
    public function findId()
42
    {
43
        if (null === $this->id) {
44
            $this->id = $this->resolveId();
45
        }
46
47
        return $this->id;
48
    }
49
50
    /**
51
     * @return string|null
52
     */
53
    protected function resolveId()
54
    {
55
        $fieldId = $this->getFieldId();
56
        $elementId = $this->getElementId();
57
58
        if (null === $fieldId || null === $elementId) {
59
            return null;
60
        }
61
62
        if (!$objectId = ObjectAssociation::find()
63
            ->select(['objectId'])
64
            ->field($fieldId)
65
            ->element($elementId)
66
            ->siteId(SiteHelper::ensureSiteId($this->getSiteId()))
67
            ->scalar()) {
68
            return null;
69
        }
70
71
        return (string)$objectId;
72
    }
73
}
74