Passed
Push — main ( 91a5ab...2b6458 )
by Seth
08:55 queued 06:17
created

RenderTranslationsCommand::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 2
nop 0
dl 0
loc 26
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
namespace SaasReady\Commands;
4
5
use Illuminate\Console\Command;
6
use SaasReady\Constants\LanguageCode;
7
use SaasReady\Models\Language;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SaasReady\Models\Translation;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\Translation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class RenderTranslationsCommand extends Command
11
{
12
    protected $signature = 'saas-ready:render-translation {langCode}';
13
    protected $description = '[SaaS Ready] Render translation file for a specific Language';
14
15
    public function handle(): void
16
    {
17
        $langCode = $this->argument('langCode');
18
        $language = Language::findByCode(LanguageCode::tryFrom($langCode));
19
20
        if (!$language || !$language->activated_at) {
21
            $this->error('Language not found or not activated');
22
23
            return;
24
        }
25
26
        // key => text
27
        $translatedTexts = Translation::all(['key', 'translations'])
28
            ->mapWithKeys(function (Translation $translation) use ($langCode) {
29
                return [
30
                    $translation->key => $translation->translations[$langCode] ?? '',
31
                ];
32
            });
33
34
        $langFilePath = lang_path("$langCode.json");
35
36
        // write
37
        file_put_contents($langFilePath, $translatedTexts);
38
39
        $this->info("All translations of $langCode is exported successfully.");
40
        $this->info('Path: ' . $langFilePath);
41
    }
42
}
43