AbstractEndpoint   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 37
ccs 12
cts 13
cp 0.9231
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A httpClient() 0 15 4
A __construct() 0 3 1
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