Completed
Push — develop ( 1cb007...29aa46 )
by Nate
04:05
created

AccessTokenConnection::getFromValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\hubspot\patron\connections;
10
11
use flipbox\hubspot\connections\IntegrationConnectionInterface;
12
use Flipbox\OAuth2\Client\Provider\HubSpotResourceOwner;
13
use yii\base\BaseObject;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class AccessTokenConnection extends BaseObject implements IntegrationConnectionInterface
20
{
21
    use traits\AccessTokenAuthorizationTrait;
22
23
    /**
24
     * @var string
25
     */
26
    private $hubId;
27
28
    /**
29
     * @var string
30
     */
31
    private $appId;
32
33
    /**
34
     * @var HubSpotResourceOwner
35
     */
36
    private $resourceOwner;
37
38
    /**
39
     * @inheritdoc
40
     * @throws \flipbox\ember\exceptions\NotFoundException
41
     * @throws \yii\base\InvalidConfigException
42
     */
43
    public function getHubId(): string
44
    {
45
        if ($this->hubId === null) {
46
            if (null === ($hubId = $this->getFromValues('hubId'))) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $hubId is correct as $this->getFromValues('hubId') (which targets flipbox\hubspot\patron\c...ection::getFromValues()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
                $hubId = $this->getResourceOwner()->getHubId();
48
            }
49
50
            $this->hubId = $hubId;
51
        }
52
53
        return $this->hubId;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     * @throws \flipbox\ember\exceptions\NotFoundException
59
     * @throws \yii\base\InvalidConfigException
60
     */
61
    public function getAppId(): string
62
    {
63
        if ($this->appId === null) {
64
            if (null === ($appId = $this->getFromValues('appId'))) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $appId is correct as $this->getFromValues('appId') (which targets flipbox\hubspot\patron\c...ection::getFromValues()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
                $appId = $this->getResourceOwner()->getAppId();
66
            }
67
68
            $this->appId = $appId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $appId can also be of type integer. However, the property $appId is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        }
70
71
        return $this->appId;
72
    }
73
74
    /**
75
     * @param string $attribute
76
     * @return null
77
     * @throws \flipbox\ember\exceptions\NotFoundException
78
     * @throws \yii\base\InvalidConfigException
79
     */
80
    protected function getFromValues(string $attribute)
81
    {
82
        $values = $this->getAccessToken()->getValues();
83
        return $values[$attribute] ?? null;
84
    }
85
86
    /**
87
     * @return HubSpotResourceOwner
88
     * @throws \flipbox\ember\exceptions\NotFoundException
89
     * @throws \yii\base\InvalidConfigException
90
     */
91
    protected function getResourceOwner()
92
    {
93
        if ($this->resourceOwner === null) {
94
            $this->resourceOwner = $this->getProvider()->getResourceOwner($this->getAccessToken());
95
        }
96
97
        return $this->resourceOwner;
98
    }
99
}
100