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