Completed
Push — master ( 8c95bb...199f9c )
by Nate
15:05
created

Contact::findElementByContactId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
3
namespace flipbox\hubspot\services;
4
5
use Craft;
6
use craft\base\ElementInterface;
7
use craft\db\Query;
8
use flipbox\hubspot\records\Contact as ContactRecord;
9
use yii\base\Component;
10
use yii\base\Exception;
11
12
class Contact extends Component
13
{
14
15
    /**
16
     * @param string $ContactId
17
     * @return ElementInterface
18
     * @throws Exception
19
     */
20
    public function getElementByContactId(string $ContactId)
21
    {
22
        if (!$element = $this->findElementByContactId($ContactId)) {
23
            throw new Exception("Unable to get Contact");
24
        }
25
26
        return $element;
27
    }
28
29
    /**
30
     * @param string $ContactId
31
     * @return ElementInterface|null
32
     */
33
    public function findElementByContactId(string $ContactId)
34
    {
35
        if (!$elementId = (new Query())
36
            ->select(['elementId'])
37
            ->from([ContactRecord::tableName()])
38
            ->where([
39
                'hubspotId' => $ContactId
40
            ])
41
            ->scalar()) {
42
            return null;
43
        }
44
45
        return Craft::$app->getUsers()->getUserById($elementId);
46
    }
47
48
    /**
49
     * Find the Contact Id by Element Id
50
     *
51
     * @param int $id
52
     * @return string|null
53
     */
54
    public function findContactIdByElementId(int $id)
55
    {
56
        $hubspotId = (new Query())
57
            ->select(['hubspotId'])
58
            ->from([ContactRecord::tableName()])
59
            ->where([
60
                'elementId' => $id
61
            ])
62
            ->scalar();
63
64
        if (!$hubspotId) {
65
            return null;
66
        }
67
68
        return $hubspotId;
69
    }
70
71
    /**
72
     * @param ElementInterface $element
73
     * @return bool
74
     */
75
    public function disassociate(
76
        ElementInterface $element
77
    ) {
78
        Craft::$app->getDb()->createCommand()
79
            ->delete(
80
                ContactRecord::tableName(),
81
                [
82
                    'elementId' => $element->getId()
83
                ]
84
            )
85
            ->execute();
86
87
        return true;
88
    }
89
90
    /**
91
     * @param int $elementId
92
     * @param string $ContactId
93
     * @return bool|array
94
     */
95
    public function associateByIds(
96
        int $elementId,
97
        string $ContactId
98
    ) {
99
100
        $record = new ContactRecord([
101
            'elementId' => $elementId,
102
            'hubspotId' => $ContactId
103
        ]);
104
105
        if (!$record->save()) {
106
            return $record->getFirstErrors();
107
        }
108
109
        return true;
110
    }
111
}
112