IdAttributeFromElementTrait::getFieldId()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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\Salesforce\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