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

Company::disassociate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 1
eloc 8
nc 1
nop 1
crap 2
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\Company as CompanyRecord;
9
use yii\base\Component;
10
use yii\base\Exception;
11
12
class Company extends Component
13
{
14
15
    /**
16
     * @param string $CompanyId
17
     * @return ElementInterface
18
     * @throws Exception
19
     */
20
    public function getElementByCompanyId(string $CompanyId)
21
    {
22
        if (!$element = $this->findElementByCompanyId($CompanyId)) {
23
            throw new Exception("Unable to get company");
24
        }
25
26
        return $element;
27
    }
28
29
    /**
30
     * @param string $CompanyId
31
     * @return ElementInterface|null
32
     */
33
    public function findElementByCompanyId(string $CompanyId)
34
    {
35
        if (!$elementId = (new Query())
36
            ->select(['elementId'])
37
            ->from([CompanyRecord::tableName()])
38
            ->where([
39
                'hubspotId' => $CompanyId
40
            ])
41
            ->scalar()) {
42
            return null;
43
        }
44
45
        return Craft::$app->getUsers()->getUserById($elementId);
46
    }
47
48
    /**
49
     * Find the Company Id by Element Id
50
     *
51
     * @param int $id
52
     * @return string|null
53
     */
54
    public function findCompanyIdByElementId(int $id)
55
    {
56
        $hubspotId = (new Query())
57
            ->select(['hubspotId'])
58
            ->from([CompanyRecord::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
                CompanyRecord::tableName(),
81
                [
82
                    'elementId' => $element->getId()
83
                ]
84
            )
85
            ->execute();
86
87
        return true;
88
    }
89
90
    /**
91
     * @param int $elementId
92
     * @param string $companyId
93
     * @return bool|array
94
     */
95
    public function associateByIds(
96
        int $elementId,
97
        string $companyId
98
    ) {
99
100
        $record = new CompanyRecord([
101
            'elementId' => $elementId,
102
            'hubspotId' => $companyId
103
        ]);
104
105
        if (!$record->save()) {
106
            return $record->getFirstErrors();
107
        }
108
109
        return true;
110
    }
111
}
112