LocaliseAPI::pullLangs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 31
ccs 0
cts 11
cp 0
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use GuzzleHttp\Client;
6
7
class LocaliseAPI
8
{
9
    protected $apiKey;
10
    protected $projectId;
11
    protected $urlBase = "https://api.lokalise.co/api";
12
13
    /**
14
     * LocaliseAPI constructor.
15
     */
16 5
    public function __construct()
17
    {
18 5
        $this->apiKey = env('LOCALISE_API_KEY');
19 5
        $this->projectId = env('LOCALISE_PROJECT');
20 5
    }
21
22
23
    /**
24
     * Return all Langs in Localise
25
     * @return array
26
     */
27 5
    public function listLangsInProject()
28
    {
29
        try {
30 5
            $body = (new Client())
31 5
                ->request('GET', $this->urlBase . '/language/list?api_token=' . $this->apiKey . '&id=' . $this->projectId)
32 5
                ->getBody();
33 5
            return json_decode($body)->languages;
34 5
        } catch (\Exception $e) {
35 5
            return [];
36
        }
37
    }
38
39
    /**
40
     * Send translations to to Localize.co
41
     * @return mixed
42
     */
43
    public function pullLangs()
44
    {
45
        $client = new Client(['verify' => false]);
46
        $response = $client->post('https://api.lokalise.co/api/project/export', [
47
            'multipart' => [
48
                [
49
                    'name' => 'api_token',
50
                    'contents' => $this->apiKey
51
                ],
52
                [
53
                    'name' => 'id',
54
                    'contents' => $this->projectId
55
                ],
56
                [
57
                    'name' => 'type',
58
                    'contents' => 'php'
59
                ],
60
                [
61
                    'name' => 'use_original',
62
                    'contents' => '1'
63
                ],
64
                [
65
                    'name' => 'export_empty',
66
                    'contents' => 'skip'
67
                ],
68
            ],
69
        ]);
70
71
        $body = $response->getBody();
72
        $details = json_decode($body);
73
        return $details->bundle->file;
74
    }
75
}