Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

Connections::findIntegration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.7333
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\craft\hubspot\services;
10
11
use flipbox\craft\hubspot\connections\ApplicationKeyConnection;
12
use flipbox\craft\hubspot\events\RegisterConnectionTypesEvent;
13
use flipbox\craft\hubspot\HubSpot;
14
use flipbox\craft\hubspot\records\Connection;
15
use flipbox\craft\integration\exceptions\ConnectionNotFound;
16
use flipbox\craft\integration\services\IntegrationConnections;
17
use Flipbox\HubSpot\Connections\ConnectionInterface;
18
use Flipbox\HubSpot\Connections\IntegrationConnectionInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Connections extends IntegrationConnections
25
{
26
    /**
27
     * The integration connection handle
28
     */
29
    const INTEGRATION_CONNECTION = 'token';
30
31
    /**
32
     * The default connection identifier
33
     */
34
    const DEFAULT_INTEGRATION_CONNECTION = 'DEFAULT_INTEGRATION';
35
36
    /**
37
     * The override file
38
     */
39
    public $overrideFile = 'hubspot-connections';
40
41
    /**
42
     * @inheritdoc
43
     */
44
    protected static function tableName(): string
45
    {
46
        return Connection::tableName();
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    protected static function connectionInstance(): string
53
    {
54
        return ConnectionInterface::class;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    protected function getDefaultConnection(): string
61
    {
62
        return HubSpot::getInstance()->getSettings()->getDefaultConnection();
63
    }
64
65
    /**
66
     * @param string $handle
67
     * @param bool $enabledOnly
68
     * @return IntegrationConnectionInterface|null
69
     */
70
    public function findIntegration(
71
        string $handle = self::DEFAULT_INTEGRATION_CONNECTION,
72
        bool $enabledOnly = true
73
    ) {
74
        if ($handle === self::DEFAULT_INTEGRATION_CONNECTION) {
75
            $handle = HubSpot::getInstance()->getSettings()->getDefaultIntegrationConnection();
76
        }
77
78
        $connection = $this->find($handle, $enabledOnly);
79
80
        if (!$connection instanceof IntegrationConnectionInterface) {
81
            return null;
82
        }
83
84
        return $connection;
85
    }
86
87
    /**
88
     * @param string $handle
89
     * @param bool $enabledOnly
90
     * @return IntegrationConnectionInterface
91
     * @throws ConnectionNotFound
92
     */
93
    public function getIntegration(
94
        string $handle = self::DEFAULT_INTEGRATION_CONNECTION,
95
        bool $enabledOnly = true
96
    ): IntegrationConnectionInterface {
97
        if (null === ($connection = $this->findIntegration($handle, $enabledOnly))) {
98
            throw new ConnectionNotFound('Unable to find connection');
99
        }
100
101
        return $connection;
102
    }
103
104
    /**
105
     * @var ConnectionInterface[]
106
     */
107
    private $types;
108
109
    /**
110
     * @return ConnectionInterface[]
111
     */
112
    public function getTypes(): array
113
    {
114
        if ($this->types === null) {
115
            $event = new RegisterConnectionTypesEvent([
116
                'types' => [
117
                    ApplicationKeyConnection::class
118
                ]
119
            ]);
120
121
            $this->trigger(
122
                $event::REGISTER_CONNECTIONS,
123
                $event
124
            );
125
126
            $this->types = $event->types;
127
        }
128
129
        return $this->types;
130
    }
131
}
132