AbstractEndpoint::httpClient()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 10
1
<?php
2
3
namespace LaravelWorkcast\RegistrationEndpoints;
4
5
use Illuminate\Http\Client\PendingRequest;
6
use Illuminate\Http\Client\Response;
7
use Illuminate\Support\Facades\Http;
8
use LaravelWorkcast\AccessToken;
9
use LaravelWorkcast\Auth;
10
use LaravelWorkcast\WorkcastException;
11
12
abstract class AbstractEndpoint
13
{
14
    protected Auth $auth;
15
16
    /**
17
     * AbstractEndpoint constructor.
18
     *
19
     * @param Auth $auth
20
     */
21 1
    public function __construct(Auth $auth)
22
    {
23 1
        $this->auth = $auth;
24
    }
25
26
    abstract public function send(): Response;
27
28
    /**
29
     * @return PendingRequest
30
     * @throws WorkcastException
31
     */
32 1
    public function httpClient(): PendingRequest
33
    {
34 1
        $token = $this->auth->getToken();
35 1
        if (!$token || !($token instanceof AccessToken) || !$token->valid()) {
36
            throw new WorkcastException('Token not valid');
37
        }
38
39 1
        return Http::withToken($token->getToken())
40 1
            ->withOptions(array_merge(
41 1
                config('workcast.http_registration_config', []),
42 1
                [
43 1
                    'base_uri' => config('workcast.registration_url'),
44 1
                ]
45 1
            ));
46
    }
47
}
48