Completed
Push — master ( 37c0e6...5c203e )
by Nate
08:56 queued 06:36
created

Connections::getDefaultConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\HubSpot;
12
use flipbox\craft\hubspot\records\Connection;
13
use flipbox\craft\integration\exceptions\ConnectionNotFound;
14
use flipbox\craft\integration\services\IntegrationConnections;
15
use Flipbox\HubSpot\Connections\ConnectionInterface;
16
use Flipbox\HubSpot\Connections\IntegrationConnectionInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Connections extends IntegrationConnections
23
{
24
    /**
25
     * The integration connection handle
26
     */
27
    const INTEGRATION_CONNECTION = 'token';
28
29
    /**
30
     * The default connection identifier
31
     */
32
    const DEFAULT_INTEGRATION_CONNECTION = 'DEFAULT_INTEGRATION';
33
34
    /**
35
     * The override file
36
     */
37
    public $overrideFile = 'hubspot-connections';
38
39
    /**
40
     * @inheritdoc
41
     */
42
    protected static function tableName(): string
43
    {
44
        return Connection::tableName();
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected static function connectionInstance(): string
51
    {
52
        return ConnectionInterface::class;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    protected function getDefaultConnection(): string
59
    {
60
        return HubSpot::getInstance()->getSettings()->getDefaultConnection();
61
    }
62
63
    /**
64
     * @param string $handle
65
     * @param bool $enabledOnly
66
     * @return IntegrationConnectionInterface|null
67
     */
68
    public function findIntegration(
69
        string $handle = self::DEFAULT_INTEGRATION_CONNECTION,
70
        bool $enabledOnly = true
71
    ) {
72
        if ($handle === self::DEFAULT_INTEGRATION_CONNECTION) {
73
            $handle = HubSpot::getInstance()->getSettings()->getDefaultIntegrationConnection();
74
        }
75
76
        $connection = $this->find($handle, $enabledOnly);
77
78
        if (!$connection instanceof IntegrationConnectionInterface) {
79
            return null;
80
        }
81
82
        return $connection;
83
    }
84
85
    /**
86
     * @param string $handle
87
     * @param bool $enabledOnly
88
     * @return IntegrationConnectionInterface
89
     * @throws ConnectionNotFound
90
     */
91
    public function getIntegration(
92
        string $handle = self::DEFAULT_INTEGRATION_CONNECTION,
93
        bool $enabledOnly = true
94
    ): IntegrationConnectionInterface {
95
        if (null === ($connection = $this->findIntegration($handle, $enabledOnly))) {
96
            throw new ConnectionNotFound('Unable to find connection');
97
        }
98
99
        return $connection;
100
    }
101
}
102