GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( a93cc2...4d85e3 )
by Gabor
02:13
created

LokaliseClient::isReady()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace ProcyonWeb\TranslationGenerator;
4
5
use GuzzleHttp\Client;
6
use Lokalise\LokaliseApiClient;
7
use Symfony\Component\Console\Output\ConsoleOutput;
8
use ZipArchive;
9
10
class LokaliseClient
11
{
12
    /** @var LokaliseApiClient */
13
    private $client;
14
15
    /** @var string|null */
16
    private $projectId;
17
    /**
18
     * @var ConsoleOutput
19
     */
20
    private $output;
21
22
    public function __construct(ConsoleOutput $output)
23
    {
24
        $this->output = $output;
25
        $apiToken = config('translation.lokalise.apiToken');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $apiToken = /** @scrutinizer ignore-call */ config('translation.lokalise.apiToken');
Loading history...
26
        $projectId = config('translation.lokalise.projectId');
27
28
        if ($apiToken && $projectId) {
29
            $this->client = new LokaliseApiClient($apiToken);
30
            $this->projectId = $projectId;
31
        }
32
    }
33
34
    public function isReady(): bool
35
    {
36
        return $this->client !== null;
37
    }
38
39
    public function uploadFile(string $data, string $filename, string $langIso): bool
40
    {
41
        try {
42
            $response = $this->client->files->upload(
43
                $this->projectId,
44
                [
45
                    'data' => base64_encode($data),
46
                    'filename' => $filename,
47
                    'lang_iso' => $langIso,
48
                    'slashn_to_linebreak' => true,
49
                ]
50
            );
51
            $result = $response->getContent()['result'];
52
            $this->output->writeln(
53
                sprintf(
54
                    '[LOKALISE] %s file uploaded. %d skipped, %d inserted, %d updated',
55
                    $filename,
56
                    $result['skipped'],
57
                    $result['inserted'],
58
                    $result['updated']
59
                )
60
            );
61
62
            return true;
63
        } catch (\Lokalise\Exceptions\LokaliseResponseException $e) {
64
            $this->output->writeln(
65
                sprintf(
66
                    '[LOKALISE] Failed to upload file: %s. %s',
67
                    $filename,
68
                    $e->getMessage()
69
                )
70
            );
71
        }
72
73
        return false;
74
    }
75
76
    public function downloadFiles(): bool
77
    {
78
        try {
79
            $response = $this->client->files->download(
80
                $this->projectId,
81
                [
82
                    'format' => 'json',
83
                    'original_filenames' => false,
84
                    'bundle_structure' => '%LANG_ISO%.%FORMAT%',
85
                    'export_empty_as' => 'base',
86
                ]
87
            );
88
            $result = $response->getContent()['result'];
89
            $bundleUrl = $result['bundle_url'];
90
91
            $this->output->writeln(sprintf('[LOKALISE] Downloading translations from bundle url: %s', $bundleUrl));
92
93
            $path = storage_path('app').'locale.zip';
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            $path = /** @scrutinizer ignore-call */ storage_path('app').'locale.zip';
Loading history...
94
            $file = fopen($path, 'wb');
95
            $client = new Client();
96
            $client->get($bundleUrl, ['save_to' => $file]);
97
98
            $zip = new ZipArchive;
99
            $zip->open($path);
100
            $zip->extractTo(resource_path('lang'));
0 ignored issues
show
Bug introduced by
The function resource_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            $zip->extractTo(/** @scrutinizer ignore-call */ resource_path('lang'));
Loading history...
101
            $zip->close();
102
103
            return true;
104
        } catch (\Throwable $e) {
105
            $this->output->writeln(
106
                sprintf(
107
                    '[LOKALISE] Failed to download files. %s',
108
                    $e->getMessage()
109
                )
110
            );
111
        }
112
113
        return false;
114
    }
115
}