Completed
Push — master ( 695b53...13261c )
by Tomas
03:17
created

AbstractApi   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createRequest() 0 7 1
A createStream() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OneSignal;
6
7
use OneSignal\Exception\InvalidArgumentException;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\StreamInterface;
10
11
abstract class AbstractApi
12
{
13
    protected $client;
14
15
    public function __construct(OneSignal $client)
16
    {
17
        $this->client = $client;
18
    }
19
20
    protected function createRequest(string $method, string $uri): RequestInterface
21
    {
22
        $request = $this->client->getRequestFactory()->createRequest($method, OneSignal::API_URL.$uri);
23
        $request = $request->withHeader('Accept', 'application/json');
24
25
        return $request;
26
    }
27
28
    protected function createStream($value, int $flags = null, int $maxDepth = 512): StreamInterface
29
    {
30
        $flags = $flags ?? (JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION);
31
32
        try {
33
            $value = json_encode($value, $flags | \JSON_THROW_ON_ERROR, $maxDepth);
34
        } catch (\JsonException $e) {
35
            throw new InvalidArgumentException("Invalid value for json encoding: {$e->getMessage()}.");
36
        }
37
38
        return $this->client->getStreamFactory()->createStream($value);
39
    }
40
}
41