ApplicationKeyConnection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 117
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A displayName() 0 4 1
A getSettingsHtml() 0 9 1
A rules() 0 25 1
A getHubId() 0 4 1
A prepareAuthorizationRequest() 0 24 2
A handleAuthorizationResponse() 0 7 1
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\connections;
10
11
use Craft;
12
use flipbox\craft\integration\connections\AbstractSaveableConnection;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class ApplicationKeyConnection extends AbstractSaveableConnection implements SavableConnectionInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    public $key;
26
27
    /**
28
     * @var string
29
     */
30
    public $hubId;
31
32
    /**
33
     * Returns the display name of this class.
34
     *
35
     * @return string The display name of this class.
36
     */
37
    public static function displayName(): string
38
    {
39
        return 'Application Key';
40
    }
41
42
    /**
43
     * @inheritdoc
44
     * @throws \Twig_Error_Loader
45
     * @throws \yii\base\Exception
46
     */
47
    public function getSettingsHtml()
48
    {
49
        return Craft::$app->getView()->renderTemplate(
50
            'hubspot/_components/connections/applicationKey',
51
            [
52
                'connection' => $this
53
            ]
54
        );
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function rules()
61
    {
62
        return array_merge(
63
            parent::rules(),
64
            [
65
                [
66
                    [
67
                        'key',
68
                        'hubId'
69
                    ],
70
                    'required'
71
                ],
72
                [
73
                    [
74
                        'key',
75
                        'hubId'
76
                    ],
77
                    'safe',
78
                    'on' => [
79
                        static::SCENARIO_DEFAULT
80
                    ]
81
                ]
82
            ]
83
        );
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getHubId(): string
90
    {
91
        return $this->hubId;
92
    }
93
94
    /**
95
     * Add the 'hapikey' to the query
96
     *
97
     * @inheritdoc
98
     */
99
    public function prepareAuthorizationRequest(
100
        RequestInterface $request
101
    ): RequestInterface {
102
103
        // Requested URI
104
        $uri = $request->getUri();
105
106
        // Get Query
107
        $query = $uri->getQuery();
108
109
        // Append to?
110
        if (!empty($query)) {
111
            $query .= '&';
112
        }
113
114
        // Add our key
115
        $query .= http_build_query([
116
            'hapikey' => $this->key
117
        ]);
118
119
        return $request->withUri(
120
            $uri->withQuery($query)
121
        );
122
    }
123
124
    /**
125
     * We can't do much, just return the response
126
     *
127
     * @inheritdoc
128
     */
129
    public function handleAuthorizationResponse(
130
        ResponseInterface $response,
131
        RequestInterface $request,
132
        callable $callable
133
    ): ResponseInterface {
134
        return $response;
135
    }
136
}
137