1 | <?php |
||
2 | |||
3 | /** |
||
4 | * MIT License |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace SprykerEco\Zed\Ratepay\Business\Internal; |
||
9 | |||
10 | use Generated\Shared\Transfer\LocaleTransfer; |
||
11 | use SprykerEco\Zed\Ratepay\Dependency\Facade\RatepayToGlossaryInterface; |
||
12 | use SprykerEco\Zed\Ratepay\RatepayConfig; |
||
13 | use Symfony\Component\Yaml\Yaml; |
||
14 | |||
15 | class Install implements InstallInterface |
||
16 | { |
||
17 | public const CREATED = 'created'; |
||
18 | public const TRANSLATIONS = 'translations'; |
||
19 | public const TRANSLATION = 'translation'; |
||
20 | public const UPDATED = 'updated'; |
||
21 | public const TEXT = 'text'; |
||
22 | /** |
||
23 | * @var \SprykerEco\Zed\Ratepay\Dependency\Facade\RatepayToGlossaryInterface |
||
24 | */ |
||
25 | protected $glossary; |
||
26 | |||
27 | /** |
||
28 | * @var \SprykerEco\Zed\Ratepay\RatepayConfig |
||
29 | */ |
||
30 | protected $config; |
||
31 | |||
32 | /** |
||
33 | * @param \SprykerEco\Zed\Ratepay\Dependency\Facade\RatepayToGlossaryInterface $glossary |
||
34 | * @param \SprykerEco\Zed\Ratepay\RatepayConfig $config |
||
35 | */ |
||
36 | public function __construct(RatepayToGlossaryInterface $glossary, RatepayConfig $config) |
||
37 | { |
||
38 | $this->glossary = $glossary; |
||
39 | $this->config = $config; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function install() |
||
46 | { |
||
47 | $fileName = $this->config->getTranslationFilePath(); |
||
48 | $translations = $this->parseYamlFile($fileName); |
||
49 | |||
50 | return $this->installKeysAndTranslations($translations); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $filePath |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | protected function parseYamlFile($filePath) |
||
59 | { |
||
60 | return Yaml::parse(file_get_contents($filePath)); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param array $translations |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | protected function installKeysAndTranslations(array $translations) |
||
69 | { |
||
70 | $results = []; |
||
71 | foreach ($translations as $keyName => $data) { |
||
72 | $results[$keyName][self::CREATED] = false; |
||
73 | if (!$this->glossary->hasKey($keyName)) { |
||
74 | $this->glossary->createKey($keyName); |
||
75 | $results[$keyName][self::CREATED] = true; |
||
76 | } |
||
77 | |||
78 | foreach ($data[self::TRANSLATIONS] as $localeName => $text) { |
||
79 | $results[$keyName][self::TRANSLATION][$localeName] = $this->addTranslation($localeName, $keyName, $text); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $results; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $localeName |
||
88 | * @param string $keyName |
||
89 | * @param string $text |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function addTranslation($localeName, $keyName, $text) |
||
94 | { |
||
95 | $locale = new LocaleTransfer(); |
||
96 | $locale->setLocaleName($localeName); |
||
97 | $translation = []; |
||
98 | |||
99 | $translation[self::TEXT] = $text; |
||
100 | $translation[self::CREATED] = false; |
||
101 | $translation[self::UPDATED] = false; |
||
102 | |||
103 | if (!$this->glossary->hasTranslation($keyName, $locale)) { |
||
104 | $this->glossary->createAndTouchTranslation($keyName, $locale, $text, true); |
||
105 | $translation[self::CREATED] = true; |
||
106 | |||
107 | return $translation; |
||
108 | } |
||
109 | |||
110 | $this->glossary->updateAndTouchTranslation($keyName, $locale, $text, true); |
||
111 | $translation[self::UPDATED] = true; |
||
112 | |||
113 | return $translation; |
||
114 | } |
||
115 | } |
||
116 |