Passed
Push — master ( 9a9997...0e6097 )
by Alexander
02:27
created

Extractor::addMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
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
11
/**
12
 * Extracts translator IDs from files within a given path and writes them into message source given merging
13
 * results with what is already there.
14
 */
15
final class Extractor
16
{
17
    /** @var string[]|null */
18
    private ?array $except = null;
19
20
    /** @var string[]|null */
21
    private ?array $only = null;
22
23
    /**
24
     * @var CategorySource[] Array of category message sources indexed by category names.
25
     */
26
    private array $categorySources = [];
27
28
    /**
29
     * @param CategorySource[] $categories
30
     */
31 15
    public function __construct(array $categories)
32
    {
33 15
        foreach ($categories as $category) {
34 15
            $this->categorySources[$category->getName()] = $category;
35
        }
36 15
    }
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 8
    public function setExcept(array $except): void
46
    {
47 8
        if (!empty($except)) {
48 2
            $this->except = $except;
49
        }
50 8
    }
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 8
    public function setOnly(array $only): void
60
    {
61 8
        if (!empty($only)) {
62 2
            $this->only = $only;
63
        }
64 8
    }
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 15
    public function process(string $filesPath, string $defaultCategory, array $languages, OutputInterface $output): void
73
    {
74 15
        if (!isset($this->categorySources[$defaultCategory])) {
75 1
            $output->writeln('<comment>Default category was not found in a list of Categories.</comment>');
76 1
            return;
77
        }
78
79 14
        $translationExtractor = new TranslationExtractor($filesPath, $this->only, $this->except);
80
81 14
        $messagesList = $translationExtractor->extract($defaultCategory);
82
83 14
        if (empty($messagesList)) {
84 6
            $output->writeln('<comment>Messages not found</comment>');
85 6
            return;
86
        }
87
88 8
        $output->writeln('Languages: ' . implode(', ', $languages));
89
90
        /**
91
         * @var string $categoryName
92
         * @var array<array-key, array<string, string>|mixed> $messages
93
         */
94 8
        foreach ($messagesList as $categoryName => $messages) {
95 8
            $output->writeln('<info>Category: "' . $categoryName . '", messages found: ' . count($messages) . '</info>');
96
97
            /** @var array<string, array<string, string>> $convertedMessages */
98 8
            $convertedMessages = $this->convert($messages);
99 8
            foreach ($languages as $language) {
100 8
                $extractCategory = isset($this->categorySources[$categoryName]) ? $categoryName : $defaultCategory;
101 8
                $this->addMessages($extractCategory, $language, $convertedMessages);
102
            }
103
        }
104 8
    }
105
106 8
    private function addMessages(string $categoryName, string $language, array $messages): void
107
    {
108 8
        $readMessages = $this->categorySources[$categoryName]->readMessages($categoryName, $language);
109
        /** @var array<string, array<string, string>> $convertedMessages */
110 8
        $convertedMessages = array_merge($messages, $readMessages);
111 8
        $this->categorySources[$categoryName]->writeMessages($categoryName, $language, $convertedMessages);
112 8
    }
113
114 8
    private function convert(array $messages): array
115
    {
116 8
        $returningMessages = [];
117
118
        /** @var array<string, string> $messages */
119 8
        foreach ($messages as $message) {
120 8
            $returningMessages[$message] = ['message' => $message];
121
        }
122
123 8
        return $returningMessages;
124
    }
125
}
126