AbstractEndpoint::httpClient()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

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