Passed
Push — ft/package ( 5ee474...180175 )
by Philippe
05:16 queued 12s
created

TranslatableCommand::isCompletelyEmpty()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\Common\Translatable;
4
5
trait TranslatableCommand
6
{
7
    /**
8
     * Update or create translatable fields for a translatable entity
9
     *
10
     * @param $translations
11
     * @param TranslatableContract $entity
12
     * @param array $keys pass the columns that need to be translated. these need to match the passed request keys
13
     */
14 4
    protected function saveTranslations($translations, TranslatableContract $entity, array $keys)
15
    {
16 4
        foreach ($entity->getAvailableLocales() as $available_locale) {
0 ignored issues
show
Bug introduced by
The method getAvailableLocales() does not exist on Thinktomorrow\Chief\Comm...le\TranslatableContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Comm...le\TranslatableContract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        foreach ($entity->/** @scrutinizer ignore-call */ getAvailableLocales() as $available_locale) {
Loading history...
17
            // Remove the product translation if any already exists
18
            // Translation is also removed if all fields of a translation are left empty
19 4
            if (!isset($translations[$available_locale]) or !($translation = $translations[$available_locale]) or $this->isCompletelyEmpty($keys, $translation)) {
20
                $entity->removeTranslation($available_locale);
21
                continue;
22
            }
23
24 4
            $this->updateTranslation($entity, $keys, $translation, $available_locale);
25
        }
26 4
    }
27
28
    /**
29
     * @param TranslatableContract $entity
30
     * @param array $keys
31
     * @param $translation
32
     * @param $available_locale
33
     */
34 4
    protected function updateTranslation(TranslatableContract $entity, array $keys, array $translation, $available_locale)
35
    {
36 4
        $attributes = [];
37
38 4
        foreach ($keys as $key) {
39 4
            if (isset($translation[$key])) {
40 4
                $attributes[$key] = $translation[$key];
41
            }
42
        }
43
44 4
        $entity->updateTranslation($available_locale, $attributes);
45 4
    }
46
47
    /**
48
     * Check if certain locale input submission is left empty
49
     *
50
     * @param array $keys
51
     * @param $translation
52
     * @return bool
53
     */
54 4
    protected function isCompletelyEmpty(array $keys, array $translation)
55
    {
56 4
        $is_completely_empty = true;
57
58 4
        foreach ($keys as $key) {
59 4
            if (!isset($translation[$key])) {
60
                continue;
61
            }
62
63 4
            if (trim($translation[$key])) {
64 4
                $is_completely_empty = false;
65
            }
66
        }
67
68 4
        return $is_completely_empty;
69
    }
70
}
71