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\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