Completed
Push — master ( 307e2d...f51e47 )
by Victor Hugo
02:38
created

PhraseappHttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace VictorAvelar\Phraseapp;
4
5
use GuzzleHttp\Client;
6
7
class PhraseappHttpClient extends Client
8
{
9
    /**
10
     * API main URL.
11
     *
12
     * @var string
13
     */
14
    const BASE_PATH = "https://api.phraseapp.com/v2/";
15
16
    /**
17
     * Timeout to wait for a response.
18
     *
19
     * @var int
20
     */
21
    const CLIENT_TIMEOUT = 10;
22
23
    /**
24
     * PhraseappHttpClient constructor.
25
     * @param array  $configuration
26
     */
27 6
    public function __construct(array $configuration = [])
28
    {
29 6
        $configuration = array_merge_recursive([
30 6
                'base_uri' => self::BASE_PATH,
31 6
                'timeout' => self::CLIENT_TIMEOUT,
32
                'allow_redirects' => false,
33
                'headers' => [
34
                    'Accept' => 'application/json'
35
                ]
36 6
            ], $configuration);
37 6
        parent::__construct($configuration);
38 6
    }
39
40
    /**
41
     * Create a new instance using the config defaults and the Oauth2 token auth strategy.
42
     *
43
     * @param string $phraseappToken
44
     * @return PhraseappHttpClient
45
     */
46 3
    public static function createWithToken(string $phraseappToken)
47
    {
48 3
        $config = ['headers' => ['Authorization' => "token $phraseappToken"]];
49
50 3
        return new self($config);
51
    }
52
53
    /**
54
     * Create a new instance using the config defaults and user:password auth strategy.
55
     *
56
     * @param string $user
57
     * @param string $password
58
     * @return PhraseappHttpClient
59
     */
60 3
    public static function createWithBasicAuth(string $user, string $password)
61
    {
62 3
        $config = ['headers' => ['Authorization' => [$user, $password]]];
63
64 3
        return new self($config);
65
    }
66
}
67