Api::keys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace ArianRashidi\PocketApi\Api;
3
4
use ArianRashidi\PocketApi\Pocket;
5
6
/**
7
 * Class Api
8
 *
9
 * @package ArianRashidi\PocketApi\Api
10
 */
11
abstract class Api
12
{
13
    /**
14
     * Pocket instance.
15
     *
16
     * @var Pocket
17
     */
18
    protected $pocket;
19
20
    /**
21
     * Api constructor.
22
     *
23
     * @param Pocket $pocket
24
     */
25
    public function __construct(Pocket $pocket)
26
    {
27
        $this->pocket = $pocket;
28
    }
29
30
    /**
31
     * Consumer key and access token.
32
     *
33
     * @return array
34
     */
35
    protected function keys(): array
36
    {
37
        return [
38
            'consumer_key' => $this->pocket->getConsumerKey(),
39
            'access_token' => $this->pocket->getAccessToken(),
40
        ];
41
    }
42
43
    /**
44
     * Send a post request.
45
     *
46
     * @param string $urlPath
47
     * @param array  $data
48
     * @param string $contentType
49
     *
50
     * @return \Psr\Http\Message\ResponseInterface
51
     */
52
    protected function request(string $urlPath, array $data, string $contentType = 'json')
53
    {
54
        $options = [
55
            'protocols'       => ['https'],
56
            'connect_timeout' => 15,
57
            'headers'         => [
58
                'Content-Type' => 'application/' . $contentType . '; charset=UTF8',
59
                'X-Accept'     => 'application/json',
60
            ],
61
        ];
62
63
        switch ($contentType) {
64
            case 'json':
65
                $options = $options + ['body' => json_encode($data)];
66
                break;
67
            case 'x-www-form-urlencoded':
68
                $options = $options + ['form_params' => $data];
69
                break;
70
        }
71
72
        $response = $this->pocket->getHttpClient()->post($urlPath, $options);
73
74
        return $response;
75
    }
76
}
77