Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

ApplicationKeyConnection::getSettingsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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