Completed
Pull Request — master (#1)
by Oleksandr
15:19 queued 07:44
created

Install   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseYamlFile() 0 3 1
A addTranslation() 0 21 2
A install() 0 6 1
A __construct() 0 4 1
A installKeysAndTranslations() 0 16 4
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;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\LocaleTransfer 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...
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));
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