Completed
Push — develop ( 0ca1ab...29c245 )
by Nate
03:14
created

IntegrationConnectionTrait::getConnection()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
cc 4
nc 5
nop 0
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\criteria\traits;
10
11
use flipbox\hubspot\connections\IntegrationConnectionInterface;
12
use flipbox\hubspot\HubSpot;
13
use flipbox\hubspot\services\Connections;
14
use yii\base\InvalidConfigException;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait IntegrationConnectionTrait
21
{
22
    /**
23
     * @var IntegrationConnectionInterface|string
24
     */
25
    protected $connection = Connections::DEFAULT_INTEGRATION_CONNECTION;
26
27
    /**
28
     * @param $value
29
     * @return $this
30
     */
31
    public function connection($value)
32
    {
33
        return $this->setConnection($value);
34
    }
35
36
    /**
37
     * @param $value
38
     * @return $this
39
     */
40
    public function setConnection($value)
41
    {
42
        $this->connection = $value;
43
        return $this;
44
    }
45
46
    /**
47
     * @return IntegrationConnectionInterface
48
     * @throws InvalidConfigException
49
     */
50
    public function getConnection(): IntegrationConnectionInterface
51
    {
52
        if ($this->connection instanceof IntegrationConnectionInterface) {
53
            return $this->connection;
54
        }
55
56
        if ($this->connection === null) {
57
            $this->connection = Connections::DEFAULT_INTEGRATION_CONNECTION;
58
        }
59
60
        $this->connection = HubSpot::getInstance()->getConnections()->get($this->connection);
61
62
        if (!$this->connection instanceof IntegrationConnectionInterface) {
63
            throw new InvalidConfigException(sprintf(
64
                "Integration Connection must be an instance of '%s', '%s' given.",
65
                IntegrationConnectionInterface::class,
66
                get_class($this->connection)
67
            ));
68
        }
69
70
        return $this->connection;
71
    }
72
}
73