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

Connections::getIntegration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.6666
cc 3
nc 4
nop 2
crap 12
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\services;
10
11
use flipbox\hubspot\connections\ConnectionInterface;
12
use flipbox\hubspot\connections\IntegrationConnectionInterface;
13
use flipbox\hubspot\events\RegisterConnectionsEvent;
14
use flipbox\hubspot\HubSpot;
15
use yii\base\InvalidConfigException;
16
use yii\di\ServiceLocator;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Connections extends ServiceLocator
23
{
24
    /**
25
     * @event RegisterConnectionsEvent The event that is triggered when registering connections.
26
     */
27
    const EVENT_REGISTER_CONNECTIONS = 'registerConnections';
28
29
    /**
30
     * The default connection handle
31
     */
32
    const APP_CONNECTION = 'app';
33
34
    /**
35
     * The default connection handle
36
     */
37
    const INTEGRATION_CONNECTION = 'token';
38
39
    /**
40
     * The default connection identifier
41
     */
42
    const DEFAULT_CONNECTION = 'DEFAULT_APP';
43
44
    /**
45
     * The default connection identifier
46
     */
47
    const DEFAULT_INTEGRATION_CONNECTION = 'DEFAULT_INTEGRATION';
48
49
    /**
50
     * @throws \yii\base\InvalidConfigException
51
     */
52
    public function init()
53
    {
54
        parent::init();
55
56
        $event = new RegisterConnectionsEvent([
57
            'connections' => []
58
        ]);
59
60
        $this->trigger(self::EVENT_REGISTER_CONNECTIONS, $event);
61
62
        $this->setComponents(
63
            $event->connections
64
        );
65
    }
66
67
    /**
68
     * @inheritdoc
69
     * @return ConnectionInterface
70
     */
71
    public function get($id, $throwException = true)
72
    {
73
        switch($id) {
74
            case self::DEFAULT_CONNECTION:
75
                $id = HubSpot::getInstance()->getSettings()->getDefaultConnection();
76
                break;
77
78
            case self::DEFAULT_INTEGRATION_CONNECTION:
79
                $id = HubSpot::getInstance()->getSettings()->getDefaultIntegrationConnection();
80
                break;
81
        }
82
83
        $connection = parent::get($id, $throwException);
84
85
        if (!$connection instanceof ConnectionInterface) {
86
            throw new InvalidConfigException(sprintf(
87
                "Connection '%s' must be an instance of '%s', '%s' given.",
88
                (string)$id,
89
                ConnectionInterface::class,
90
                get_class($connection)
91
            ));
92
        }
93
        return $connection;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     * @return ConnectionInterface
99
     */
100
    public function getIntegration($id, $throwException = true)
101
    {
102
        if ($id === self::DEFAULT_INTEGRATION_CONNECTION) {
103
            $id = HubSpot::getInstance()->getSettings()->getDefaultIntegrationConnection();
104
        }
105
106
        $connection = $this->get($id, $throwException);
107
108
        if (!$connection instanceof IntegrationConnectionInterface) {
109
            throw new InvalidConfigException(sprintf(
110
                "Integration Connection '%s' must be an instance of '%s', '%s' given.",
111
                (string)$id,
112
                IntegrationConnectionInterface::class,
113
                get_class($connection)
114
            ));
115
        }
116
        return $connection;
117
    }
118
119
    /**
120
     * @param bool $throwException
121
     * @return ConnectionInterface[]
122
     * @throws InvalidConfigException
123
     */
124
    public function getAll($throwException = true)
125
    {
126
        $components = [];
127
128
        foreach ($this->getComponents(true) as $id => $component) {
129
            $components[$id] = $this->get($id, $throwException);
130
        }
131
132
        return $components;
133
    }
134
}
135