Completed
Push — master ( fcbc5c...f55285 )
by
unknown
07:48
created

src/Import/ImportTranslations.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Thinktomorrow\Squanto\Import;
4
5
use Thinktomorrow\Squanto\Services\ImportStatistics;
6
use Thinktomorrow\Squanto\Services\LaravelTranslationsReader;
7
8
class ImportTranslations
9
{
10
    private $reader;
11
    private $overwriteProtection = true;
12
    private $dry = false;
13
    private $stats;
14
    private $excluded_files;
15
16 12
    public function __construct(LaravelTranslationsReader $reader, ImportStatistics $stats)
17
    {
18 12
        $this->reader = $reader;
19 12
        $this->stats = $stats;
20 12
        $this->excluded_files = config('squanto.excluded_files', []);
21 12
    }
22
23
    public function dry($enable = true)
24
    {
25
        $this->dry = !!$enable;
26
27
        return $this;
28
    }
29
30 12
    public function import($locale)
31
    {
32 12
        $this->pushSingleToStats('overwrite_protection', $this->overwriteProtection);
33
34 12
        $translations = $this->reader->read($locale, $this->excluded_files)->flatten();
35
36 12
        foreach ($translations as $key => $value) {
37 12
            $this->insertOrUpdateValue($locale, $key, $value);
38
        }
39
40 12
        return $this;
41
    }
42
43
    /**
44
     * Import an existing entry that was put on hold due to overwrite protection
45
     *
46
     * @param $locale
47
     * @param $key
48
     * @param $value
49
     * @return $this
50
     */
51 View Code Duplication
    public function importOnHoldValue($locale, $key, $value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $stats = app(ImportSingleTranslation::class)
54
            ->disableOverwriteProtection()
55
            ->dry($this->dry)
56
            ->import($locale, $key, $value)
57
            ->getStats();
58
59
        // TODO: need to test this
60
61
        $this->pushToStats(key($stats), reset($stats));
62
        $this->removeFromStats('updated_on_hold', $locale, $key);
63
64
        return $this;
65
    }
66
67 9
    public function enableOverwriteProtection($enable = true)
68
    {
69 9
        $this->overwriteProtection = $enable;
70
71 9
        return $this;
72
    }
73
74 9
    public function disableOverwriteProtection()
75
    {
76 9
        return $this->enableOverwriteProtection(false);
77
    }
78
79 12
    private function pushToStats($key, $value)
80
    {
81 12
        $this->stats->pushTranslation($key, $value);
82 12
    }
83
84
    private function removeFromStats($action, $locale, $key)
85
    {
86
        $this->stats->removeTranslation($action, $locale, $key);
87
    }
88
89 12
    private function pushSingleToStats($key, $value)
90
    {
91 12
        $this->stats->pushSingle($key, $value);
92 12
    }
93
94 9
    public function getStats()
95
    {
96 9
        return $this->stats;
97
    }
98
99
    /**
100
     * @param $locale
101
     * @param $key
102
     * @param $value
103
     */
104 12 View Code Duplication
    private function insertOrUpdateValue($locale, $key, $value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 12
        $stats = app(ImportSingleTranslation::class)
107 12
                    ->enableOverwriteProtection($this->overwriteProtection)
108 12
                    ->dry($this->dry)
109 12
                    ->import($locale, $key, $value)
110 12
                    ->getStats();
111
112 12
        $this->pushToStats(key($stats), reset($stats));
113 12
    }
114
}
115