Passed
Pull Request — master (#41)
by
unknown
12:15
created

getComposerPackageName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
use Efabrica\TranslationsAutomatization\Command\CheckDictionaries\CheckDictionariesConfig;
4
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException;
5
use GuzzleHttp\Client;
6
use Nette\Utils\FileSystem;
7
8
/**
9
 * Usage:
10
 * --params="basePath=/your/base/path&languageId=en_US&url=url_to_api&apiToken=api_token&componentId=component_id"
11
 */
12
13
if (!isset($basePath)) {
14
    return new InvalidConfigInstanceReturnedException('$basePath is not set. Use --params="basePath=/your/base/path"');
15
}
16
17
if (!isset($url)) {
18
    return new InvalidConfigInstanceReturnedException('$url is not set. Use --params="url=url_to_api"');
19
}
20
21
$params = [
22
    'api_token' => $apiToken ?? null,
23
    'component_id' => $componentId ?? getComposerPackageName($basePath),
24
    'language_id' => $languageId ?? 'en_US',
25
];
26
$client = new Client();
27
$response = $client->get($url . '?' . http_build_query($params));
28
$response = json_decode($response->getBody()->getContents(), true);
29
$directories = $response['data'];
30
return new CheckDictionariesConfig($directories);
31
32
function getComposerPackageName(string $basePath): ?string
33
{
34
    $composerJsonFilePath = $basePath . '/composer.json';
35
    if (is_file($composerJsonFilePath)) {
36
        $composerJson = FileSystem::read($composerJsonFilePath);
37
        $composerJsonData = json_decode($composerJson, true);
38
        return is_array($composerJsonData) ? (string)$composerJsonData['name'] : null;
39
    }
40
    throw new Exception('Not found composer.json on path: ' . $composerJsonFilePath);
41
}
42