GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f30caa...b87643 )
by Pascal
01:16
created

Request::validateData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace ProtoneMedia\LaravelPaddle\Api;
4
5
use Illuminate\Support\Facades\Validator;
6
use Illuminate\Support\Str;
7
8
class Request
9
{
10
    /**
11
     * Paddle API Endpoint.
12
     */
13
    const API_ENDPOINT = "https://vendors.paddle.com/api";
14
15
    /**
16
     * Method options.
17
     */
18
    const METHOD_GET  = 'get';
19
    const METHOD_POST = 'post';
20
21
    /**
22
     * @var string
23
     */
24
    private $uri;
25
26
    /**
27
     * @var array
28
     */
29
    private $data = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $rules = [];
35
36
    /**
37
     * @var string
38
     */
39
    private $method;
40
41
    /**
42
     * Creates an instance with the URI and data.
43
     *
44
     * @param string $uri
45
     * @param array  $data
46
     * @param array  $rules
47
     * @param string $method
48
     */
49
    public function __construct(string $uri, array $data = [], array $rules = [], string $method = self::METHOD_POST)
50
    {
51
        $this->uri    = $uri;
52
        $this->rules  = $rules;
53
        $this->method = $method;
54
55
        $this->fill($data);
56
    }
57
58
    /**
59
     * Formats the URL to send the request to.
60
     *
61
     * @return string
62
     */
63
    public function url(): string
64
    {
65
        return static::API_ENDPOINT . $this->uri;
66
    }
67
68
    /**
69
     * Validates the data with the rules.
70
     *
71
     * @return $this
72
     * @throws \ProtoneMedia\LaravelPaddle\Api\InvalidDataException
73
     */
74
    protected function validateData()
75
    {
76
        tap(Validator::make($this->data, $this->rules), function ($validator) {
77
            if ($validator->passes()) {
78
                return;
79
            }
80
81
            throw InvalidDataException::fromValidator($validator);
82
        });
83
84
        return $this;
85
    }
86
87
    /**
88
     * Sends the data payload to the uri and returns to decoded response.
89
     *
90
     * @return mixed
91
     *
92
     * @throws \ProtoneMedia\LaravelPaddle\Api\PaddleApiException
93
     */
94
    public function send()
95
    {
96
        $this->validateData();
97
98
        $method = $this->method;
99
100
        $data = $this->getData() + ['vendor_id' => config('paddle.vendor_id')];
101
102
        if ($method === static::METHOD_POST) {
103
            $data['vendor_auth_code'] = config('paddle.vendor_auth_code');
104
        }
105
106
        $response = app('laravel-paddle.http')
107
            ->asFormParams()
108
            ->$method($this->url(), $data);
109
110
        if (!$response->isSuccess()) {
111
            throw PaddleApiException::unsuccessfulStatus($response->status());
112
        }
113
114
        $json = $response->json();
115
116
        if ($json['success'] ?? null) {
117
            return $json['response'];
118
        }
119
120
        throw PaddleApiException::fromResponse($json);
121
    }
122
123
    /**
124
     * Getter for the set data.
125
     *
126
     * @return array
127
     */
128
    public function getData(): array
129
    {
130
        return $this->data;
131
    }
132
133
    /**
134
     * Loops through the array to set all data attributes.
135
     *
136
     * @param  array  $data
137
     * @return $this
138
     */
139
    private function fill(array $data)
140
    {
141
        foreach ($data as $key => $value) {
142
            $key = lcfirst(Str::studly($key));
143
144
            $this->$key($value);
145
        }
146
    }
147
148
    /**
149
     * Setter for the data.
150
     *
151
     * @param string $key
152
     * @param mixed $value
153
     * @return $this
154
     */
155
    protected function setAttribute(string $key, $value = null)
156
    {
157
        $this->data[Str::snake($key)] = $value;
158
159
        return $this;
160
    }
161
162
    /**
163
     * To make the request fluent.
164
     *
165
     * @param  string $method
166
     * @param  array $parameters
167
     * @return $this
168
     */
169
    public function __call($method, $parameters)
170
    {
171
        return $this->setAttribute($method, $parameters[0] ?? null);
172
    }
173
}
174