Completed
Push — master ( 2093be...99e81f )
by Ben
07:44
created

ImportSingleTranslation::updateTranslation()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 5
nop 4
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 8.439
c 1
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Squanto\Import;
4
5
use Thinktomorrow\Squanto\Domain\Line;
6
7
class ImportSingleTranslation
8
{
9
    private $overwriteProtection = true;
10
    private $dry = false;
11
12
    /**
13
     * key: actiontype, value: item array
14
     * e.g. ['inserts' => ['key' => 'foo.bar', 'new_value' => 'baz']]
15
     *
16
     * @var array
17
     */
18
    private $stats = [];
19
20
    /**
21
     * @param $locale
22
     * @param $key
23
     * @param $value
24
     * @return $this
25
     */
26 9
    public function import($locale, $key, $value)
27
    {
28 9
        if ($line = Line::findByKey($key)) {
29 6
            return $this->updateTranslation($line, $locale, $key, $value);
30
        }
31
32 9
        return $this->insertTranslation($locale, $key, $value);
33
    }
34
35
    /**
36
     * @param $locale
37
     * @param $key
38
     * @param $value
39
     * @return $this
40
     */
41 9
    private function insertTranslation($locale, $key, $value)
42
    {
43 9
        if (!$this->dry) {
44 9
            $line = Line::findOrCreateByKey($key)->saveValue($locale, $value);
45 9
            $line->saveSuggestedType();
46
        }
47
48 9
        $this->setStats('inserts', $locale, $key, $value);
49
50 9
        return $this;
51
    }
52
53
    /**
54
     * @param Line $line
55
     * @param $locale
56
     * @param $key
57
     * @param $value
58
     * @return $this
59
     */
60 6
    private function updateTranslation(Line $line, $locale, $key, $value)
61
    {
62 6
        $translation = $line->getValue($locale, false);
63
64
        // Ignore the translation if it has remained the same
65 6
        if ($translation === $value) {
66 6
            $this->setStats('remained_same', $locale, $key, $value, $translation);
67 6
            return $this;
68
        }
69
70 6
        if (!is_null($translation) && $this->overwriteProtection) {
71 3
            $this->setStats('updates_on_hold', $locale, $key, $value, $translation);
72 3
            return $this;
73
        }
74
75 3
        if ($translation) {
76 3
            if (!$this->dry) {
77 3
                $line->saveValue($locale, $value);
78
            }
79 3
            $this->setStats('updates', $locale, $key, $value, $translation);
80
81 3
            return $this;
82
        }
83
84
        return $this->insertTranslation($locale, $key, $value);
85
    }
86
87 9
    public function getStats()
88
    {
89 9
        return $this->stats;
90
    }
91
92
    /**
93
     * @param $action
94
     * @param $locale
95
     * @param $key
96
     * @param $new_value
97
     * @param null $original_value
98
     */
99 9
    private function setStats($action, $locale, $key, $new_value, $original_value = null)
100
    {
101 9
        $this->stats = [$action => new Entry($locale, $key, $new_value, $original_value)];
102 9
    }
103
104
    /**
105
     * @param bool $enable
106
     * @return $this
107
     */
108 9
    public function enableOverwriteProtection($enable = true)
109
    {
110 9
        $this->overwriteProtection = !!$enable;
111 9
        return $this;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117
    public function disableOverwriteProtection()
118
    {
119
        return $this->enableOverwriteProtection(false);
120
    }
121
122
    /**
123
     * @param bool $enable
124
     * @return $this
125
     */
126 9
    public function dry($enable = true)
127
    {
128 9
        $this->dry = !!$enable;
129 9
        return $this;
130
    }
131
}
132