Completed
Push — develop ( 639fa1...b48ec5 )
by Nate
02:32
created

resolveConnection()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 9.568
cc 4
nc 5
nop 1
crap 20
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\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 IntegrationConnectionResolverTrait
21
{
22
    /**
23
     * @param string|IntegrationConnectionInterface $connection
24
     * @return IntegrationConnectionInterface
25
     * @throws InvalidConfigException
26
     */
27
    protected function resolveConnection($connection): IntegrationConnectionInterface
28
    {
29
        if ($connection instanceof IntegrationConnectionInterface) {
30
            return $connection;
31
        }
32
33
        if ($connection === null) {
34
            $connection = Connections::DEFAULT_INTEGRATION_CONNECTION;
35
        }
36
37
        $connection = HubSpot::getInstance()->getConnections()->get($connection);
38
39
        if (!$connection instanceof IntegrationConnectionInterface) {
40
            throw new InvalidConfigException(sprintf(
41
                "Integration Connection must be an instance of '%s', '%s' given.",
42
                IntegrationConnectionInterface::class,
43
                get_class($connection)
44
            ));
45
        }
46
47
        return $connection;
48
    }
49
}
50