AbstractEndpoint::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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