Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

ProviderTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 52
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setProvider() 0 5 1
A getProvider() 0 8 2
A resolveProvider() 0 14 4
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\patron\connections\traits;
10
11
use Craft;
12
use Flipbox\OAuth2\Client\Provider\HubSpot;
13
use flipbox\patron\Patron;
14
use yii\base\InvalidArgumentException;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait ProviderTrait
21
{
22
    /**
23
     * @var mixed
24
     */
25
    private $provider;
26
27
    /**
28
     * @param $provider
29
     * @return $this
30
     */
31
    public function setProvider($provider)
32
    {
33
        $this->provider = $provider;
34
        return $this;
35
    }
36
37
    /**
38
     * @return HubSpot
39
     * @throws \flipbox\ember\exceptions\NotFoundException
40
     * @throws \yii\base\InvalidConfigException
41
     */
42
    public function getProvider(): HubSpot
43
    {
44
        if ($this->provider instanceof HubSpot) {
45
            return $this->provider;
46
        }
47
48
        return $this->provider = $this->resolveProvider($this->provider);
49
    }
50
51
    /**
52
     * @param $provider
53
     * @return HubSpot
54
     * @throws \flipbox\ember\exceptions\NotFoundException
55
     * @throws \yii\base\InvalidConfigException
56
     */
57
    protected function resolveProvider($provider): HubSpot
58
    {
59
        if (is_numeric($provider) || is_string($provider)) {
60
            $provider = Patron::getInstance()->getProviders()->get($provider);
61
        } else {
62
            $provider = Craft::createObject($provider);
63
        }
64
65
        if (!$provider instanceof HubSpot) {
66
            throw new InvalidArgumentException("Unable to resolve provider");
67
        }
68
69
        return $provider;
70
    }
71
}
72