Passed
Push — master ( f60fab...e1fa13 )
by Def
07:40 queued 11s
created

Extractor::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\TranslatorExtractor;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Yiisoft\Files\PathMatcher\PathMatcher;
9
use Yiisoft\Translator\Extractor\TranslationExtractor;
10
use Yiisoft\Translator\MessageReaderInterface;
11
use Yiisoft\Translator\MessageWriterInterface;
12
13
/**
14
 * Extracts translator IDs from files within a given path and writes them into message source given merging
15
 * results with what is already there.
16
 */
17
final class Extractor
18
{
19
    private MessageWriterInterface $messageWriter;
20
    private MessageReaderInterface $messageReader;
21
22
    /** @var string[]|null */
23
    private ?array $except = null;
24
25
    /** @var string[]|null */
26
    private ?array $only = null;
27
28
    /**
29
     * @param MessageReaderInterface $messageReader Message reader to get messages from.
30
     * @param MessageWriterInterface $messageWriter Message writer to write messages to.
31
     */
32 12
    public function __construct(MessageReaderInterface $messageReader, MessageWriterInterface $messageWriter)
33
    {
34 12
        $this->messageReader = $messageReader;
35 12
        $this->messageWriter = $messageWriter;
36 12
    }
37
38
    /**
39
     * Set list of patterns that the files or directories should not match.
40
     *
41
     * @see PathMatcher
42
     *
43
     * @param string[] $except
44
     */
45 7
    public function setExcept(array $except): void
46
    {
47 7
        if (!empty($except)) {
48 2
            $this->except = $except;
49
        }
50 7
    }
51
52
    /**
53
     * Set list of patterns that the files or directories should match.
54
     *
55
     * @see PathMatcher
56
     *
57
     * @param string[] $only
58
     */
59 7
    public function setOnly(array $only): void
60
    {
61 7
        if (!empty($only)) {
62 2
            $this->only = $only;
63
        }
64 7
    }
65
66
    /**
67
     * @param string $filesPath Path to files to extract from.
68
     * @param string $defaultCategory Category to use if category isn't set in translation call.
69
     * @param string[] $languages Languages to write extracted IDs to.
70
     * @param OutputInterface $output
71
     */
72 12
    public function process(string $filesPath, string $defaultCategory, array $languages, OutputInterface $output): void
73
    {
74 12
        $translationExtractor = new TranslationExtractor($filesPath, $this->only, $this->except);
75
76 12
        $messagesList = $translationExtractor->extract($defaultCategory);
77
78 12
        if (empty($messagesList)) {
79 6
            $output->writeln('<comment>Messages not found</comment>');
80 6
            return;
81
        }
82
83 6
        $output->writeln('Languages: ' . implode(', ', $languages));
84
85
        /**
86
         * @var string $categoryName
87
         * @var array<array-key, array<string, string>|mixed> $messages
88
         */
89 6
        foreach ($messagesList as $categoryName => $messages) {
90 6
            $output->writeln('<info>Category: "' . $categoryName . '", messages found: ' . count($messages) . '</info>');
91
92
            /** @var array<string, array<string, string>> $convertedMessages */
93 6
            $convertedMessages = $this->convert($messages);
94 6
            foreach ($languages as $language) {
95 6
                $readMessages = $this->messageReader->getMessages($categoryName, $language);
96 6
                $convertedMessages = array_merge($convertedMessages, $readMessages);
97 6
                $this->messageWriter->write($categoryName, $language, $convertedMessages);
98
            }
99
        }
100 6
    }
101
102 6
    private function convert(array $messages): array
103
    {
104 6
        $returningMessages = [];
105
106
        /** @var array<string, string> $messages */
107 6
        foreach ($messages as $message) {
108 6
            $returningMessages[$message] = ['message' => $message];
109
        }
110
111 6
        return $returningMessages;
112
    }
113
}
114