TranslationsExportCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 44
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllModels() 0 6 1
A handle() 0 24 3
A __construct() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Console;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\Plugins\TranslationsExport\Document\TranslationsExportDocument;
0 ignored issues
show
Bug introduced by
The type Thinktomorrow\Chief\Plug...nslationsExportDocument 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...
7
use Thinktomorrow\Chief\Site\Urls\UrlRecord;
8
use Thinktomorrow\Chief\Site\Visitable\Visitable;
9
10
class TranslationsExportCommand extends BaseCommand
11
{
12
    protected $signature = 'chief:translations-export
13
                                    {locale : the origin locale}
14
                                    {--target= : the locales for which you want to provde a column}';
15
    protected $description = 'Export page text content for translations';
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
    }
21
22
    public function handle(): void
23
    {
24
        $locales = config('chief.locales');
25
        $locale = $this->argument('locale');
26
27
        if (! in_array($locale, $locales)) {
28
            throw new \InvalidArgumentException('Passed locale ' . $locale .' is not found as Chief locale. Available locales are ' . implode(',', $locales));
29
        }
30
31
        $targetLocales = $locales;
32
        unset($targetLocales[array_search($locale, $targetLocales)]);
33
34
        if ($this->option('target')) {
35
            $targetLocales = explode(',', $this->option('target'));
36
        }
37
38
        $models = $this->getAllModels($locale);
39
40
        $models = $models->random(1);
41
42
        (new TranslationsExportDocument($models, $locale, $targetLocales))
43
            ->store('test.xlsx');
44
45
        $this->info('Finished export. File available at ...');
46
    }
47
48
    private function getAllModels(string $locale): Collection
49
    {
50
        return UrlRecord::allOnlineModels($locale)
51
            // In case the url is not found or present for given locale.
52
            ->reject(function (Visitable $model) use ($locale) {
53
                return ! $model->url($locale);
54
            });
55
    }
56
}
57