Client::getHttpClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Akeneo\Crowdin;
4
5
use \InvalidArgumentException;
6
use GuzzleHttp\Client as HttpClient;
7
8
/**
9
 * Simple Crowdin PHP client
10
 *
11
 * @author Nicolas Dupont <[email protected]>
12
 */
13
class Client
14
{
15
    /** @var string base url */
16
    const BASE_URL = 'https://api.crowdin.com/api/';
17
18
    /** @var HttpClient */
19
    protected $httpClient;
20
21
    /** @var string the project identifier */
22
    protected $projectIdentifier;
23
24
    /** @var string the project api key */
25
    protected $projectApiKey;
26
27
    /**
28
     * Instantiate a new Crowdin Client
29
     *
30
     * @param string $identifier the project identifier
31
     * @param string $apiKey     the project api key
32
     */
33
    public function __construct($identifier, $apiKey)
34
    {
35
        $this->projectIdentifier = $identifier;
36
        $this->projectApiKey     = $apiKey;
37
    }
38
39
    /**
40
     * @param string $method the api method
41
     *
42
     * @throws InvalidArgumentException
43
     *
44
     * @return mixed
45
     */
46
    public function api($method)
47
    {
48
        $fileReader = new FileReader();
49
        switch ($method) {
50
            case 'info':
51
                $api = new Api\Info($this);
52
                break;
53
            case 'supported-languages':
54
                $api = new Api\SupportedLanguages($this);
55
                break;
56
            case 'status':
57
                $api = new Api\Status($this);
58
                break;
59
            case 'download':
60
                $api = new Api\Download($this);
61
                break;
62
            case 'add-file':
63
                $api = new Api\AddFile($this, $fileReader);
64
                break;
65
            case 'update-file':
66
                $api = new Api\UpdateFile($this, $fileReader);
67
                break;
68
            case 'delete-file':
69
                $api = new Api\DeleteFile($this);
70
                break;
71
            case 'export':
72
                $api = new Api\Export($this);
73
                break;
74
            case 'add-directory':
75
                $api = new Api\AddDirectory($this);
76
                break;
77
            case 'delete-directory':
78
                $api = new Api\DeleteDirectory($this);
79
                break;
80
            case 'upload-translation':
81
                $api = new Api\UploadTranslation($this, $fileReader);
82
                break;
83
            case 'language-status':
84
                $api = new Api\LanguageStatus($this);
85
                break;
86
            default:
87
                throw new InvalidArgumentException(sprintf('Undefined api method "%s"', $method));
88
        }
89
90
        return $api;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getProjectIdentifier()
97
    {
98
        return $this->projectIdentifier;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getProjectApiKey()
105
    {
106
        return $this->projectApiKey;
107
    }
108
109
    /**
110
     * @return HttpClient
111
     */
112
    public function getHttpClient()
113
    {
114
        if ($this->httpClient === null) {
115
            $this->httpClient = new HttpClient(['base_uri' => self::BASE_URL]);
116
        }
117
118
        return $this->httpClient;
119
    }
120
}
121