Completed
Push — master ( 950945...268420 )
by Mads
01:14
created

Base::call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fenerum\API;
6
7
use Fenerum\API\Exceptions\FenerumValidationException;
8
use Fenerum\ApiClient;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Validator;
11
12
/**
13
 * Class Base.
14
 * @see http://docs.fenerum.com/
15
 */
16
class Base
17
{
18
    /**
19
     * @var \Fenerum\ApiClient
20
     */
21
    protected $client;
22
23
    /**
24
     * @param \Fenerum\ApiClient $client
25
     */
26
    public function __construct(ApiClient $client)
27
    {
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * @param string $method
33
     * @param string $uri
34
     * @param array|null $payload
35
     * @return array|null
36
     * @throws \Fenerum\API\Exceptions\FenerumApiException
37
     */
38
    public function call(string $method, string $uri, ?array $payload = null): ?array
39
    {
40
        return $this->client->request($method, $uri, $payload);
41
    }
42
43
    /**
44
     * @param array $data
45
     * @param array $rules
46
     * @return array
47
     * @throws \Fenerum\API\Exceptions\FenerumValidationException
48
     */
49
    protected function validate(array $data, array $rules): array
50
    {
51
        $validator = Validator::make($data, $rules);
52
        if ($validator->fails()) {
53
            $this->logger($validator->errors()->toJson());
54
55
            throw new FenerumValidationException($validator);
56
        }
57
58
        return $validator->validated();
59
    }
60
61
    /**
62
     * @param string $string
63
     */
64
    private function logger(string $string): void
65
    {
66
        if (true === config('fenerum.debug')) {
67
            Log::debug(\get_class($this) . ' ' . $string);
68
        }
69
    }
70
}
71