Passed
Push — main ( d47134...ca2b40 )
by Dante
12:54
created

TranslateFileCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 45
c 1
b 0
f 0
dl 0
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildOptionParser() 0 29 1
A execute() 0 30 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2024 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\ImportTools\Command;
17
18
use Cake\Command\Command;
19
use Cake\Console\Arguments;
20
use Cake\Console\ConsoleIo;
21
use Cake\Console\ConsoleOptionParser;
22
use Cake\Core\Configure;
23
24
/**
25
 * TranslateFile command.
26
 * Perform from a file using a translator engine.
27
 * The input file is translated from a source language to a destination language using a translator engine.
28
 * The output file is created with the translated content.
29
 * The translator engine is defined in the configuration.
30
 * The configuration must contain the translator engine class and options
31
 * I.e.:
32
 * 'Translators' => [
33
 *   'deepl' => [
34
 *      'name' => 'DeepL',
35
 *      'class' => '\BEdita\I18n\Deepl\Core\Translator',
36
 *      'options' => [
37
 *        'auth_key' => '************',
38
 *      ],
39
 *   ],
40
 * ],
41
 */
42
class TranslateFileCommand extends Command
43
{
44
    /**
45
     * @inheritDoc
46
     */
47
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
48
    {
49
        $parser->addOption('input', [
50
            'short' => 'i',
51
            'help' => 'Input file path',
52
            'required' => true,
53
        ]);
54
        $parser->addOption('output', [
55
            'short' => 'o',
56
            'help' => 'Output file path',
57
            'required' => true,
58
        ]);
59
        $parser->addOption('from', [
60
            'short' => 'f',
61
            'help' => 'Source language',
62
            'required' => true,
63
        ]);
64
        $parser->addOption('to', [
65
            'short' => 't',
66
            'help' => 'Dest language',
67
            'required' => true,
68
        ]);
69
        $parser->addOption('translator', [
70
            'short' => 'e',
71
            'help' => 'Translator engine name',
72
            'required' => true,
73
        ]);
74
75
        return $parser;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function execute(Arguments $args, ConsoleIo $io): int
82
    {
83
        $i = (string)($args->getOption('input') ?? (string)$args->getOption('i'));
84
        if (!file_exists($i)) {
85
            $io->err(sprintf('Input file "%s" does not exist', $i));
86
87
            return self::CODE_ERROR;
88
        }
89
        $o = (string)($args->getOption('output') ?? $args->getOption('o'));
90
        $f = (string)($args->getOption('from') ?? $args->getOption('f'));
91
        $t = (string)($args->getOption('to') ?? $args->getOption('t'));
92
        $e = (string)($args->getOption('translator') ?? $args->getOption('e'));
93
        $io->out(sprintf('"%s" [%s] -> "%s" [%s] using "%s" engine.', $i, $f, $o, $t, $e));
94
        $cfg = (array)Configure::read(sprintf('Translators.%s', $e));
95
        if (empty($cfg)) {
96
            $io->err(sprintf('No translator engine "%s" is set in configuration', $e));
97
98
            return self::CODE_ERROR;
99
        }
100
        $class = $cfg['class'];
101
        $options = $cfg['options'];
102
        $translator = new $class();
103
        $translator->setup($options);
104
        $translation = $translator->translate([(string)file_get_contents($i)], $f, $t);
105
        $translation = json_decode($translation, true);
106
        $translation = $translation['translation'];
107
        file_put_contents($o, $translation);
108
        $io->out('Done. Bye!');
109
110
        return self::CODE_SUCCESS;
111
    }
112
}
113