Completed
Push — master ( 4e5419...4e5419 )
by ARCANEDEV
14s queued 12s
created

TransPublisher::resetResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelLang;
6
7
use Arcanedev\LaravelLang\Contracts\TransPublisher as TransPublisherContract;
8
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerContract;
9
use Arcanedev\LaravelLang\Exceptions\LangPublishException;
10
use Illuminate\Filesystem\Filesystem;
11
use Illuminate\Support\Str;
12
use SplFileInfo;
13
14
/**
15
 * Class     TransPublisher
16
 *
17
 * @package  Arcanedev\LaravelLang
18
 * @author   ARCANEDEV <[email protected]>
19
 */
20
class TransPublisher implements TransPublisherContract
21
{
22
    /* -----------------------------------------------------------------
23
     |  Properties
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * The filesystem instance.
29
     *
30
     * @var \Illuminate\Filesystem\Filesystem
31
     */
32
    private $filesystem;
33
34
    /**
35
     * The TransManager instance.
36
     *
37
     * @var \Arcanedev\LaravelLang\Contracts\TransManager
38
     */
39
    private $manager;
40
41
    /**
42
     * Available locales.
43
     *
44
     * @var \Arcanedev\LaravelLang\Entities\LocaleCollection
45
     */
46
    private $locales;
47
48
    /**
49
     * The application lang path.
50
     *
51
     * @var string
52
     */
53
    private $langPath;
54
55
    /**
56
     * Publish's results.
57
     *
58
     * @var array
59
     */
60
    private $results = [];
61
62
    /* -----------------------------------------------------------------
63
     |  Constructor
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Make TransPublisher instance.
69
     *
70
     * @param  \Illuminate\Filesystem\Filesystem              $filesystem
71
     * @param  \Arcanedev\LaravelLang\Contracts\TransManager  $manager
72
     * @param  string                                         $langPath
73
     */
74 78
    public function __construct(Filesystem $filesystem, TransManagerContract $manager, string $langPath)
75
    {
76 78
        $this->filesystem = $filesystem;
77 78
        $this->manager    = $manager;
78 78
        $this->langPath   = realpath($langPath);
79
80 78
        $this->init();
81 78
    }
82
83
    /**
84
     * Start the engine.
85
     */
86 78
    private function init(): void
87
    {
88 78
        $this->locales = $this->manager->getCollection('vendor');
89 78
    }
90
91
    /* -----------------------------------------------------------------
92
     |  Getters & Setters
93
     | -----------------------------------------------------------------
94
     */
95
96
    /**
97
     * Get the locale destination path.
98
     *
99
     * @param  string  $locale
100
     *
101
     * @return string
102
     */
103 42
    private function getDestinationPath(string $locale): string
104
    {
105 42
        return $this->langPath.DIRECTORY_SEPARATOR.$locale;
106
    }
107
108
    /**
109
     * Get a locale from the collection.
110
     *
111
     * @param  string  $key
112
     * @param  mixed   $default
113
     *
114
     * @return \Arcanedev\LaravelLang\Entities\Locale|mixed
115
     */
116 42
    private function getLocale(string $key, $default = null)
117
    {
118 42
        return $this->locales->get($key, $default);
119
    }
120
121
    /* -----------------------------------------------------------------
122
     |  Main Methods
123
     | -----------------------------------------------------------------
124
     */
125
126
    /**
127
     * Publish a lang.
128
     *
129
     * @param  string  $locale
130
     * @param  array   $options
131
     *
132
     * @return array
133
     */
134 54
    public function publish(string $locale, array $options = []): array
135
    {
136 54
        $this->resetResults();
137
138 54
        $locale = trim($locale);
139
140 54
        if ($this->isDefault($locale)) {
141 6
            $this->results['skipped'][] = $locale;
142
143 6
            return $this->results;
144
        }
145
146 48
        $this->checkLocale($locale);
147
148 42
        $source      = $this->getLocale($locale)->getPath();
149 42
        $destination = $this->getDestinationPath($locale);
150
151 42
        $this->filesystem->ensureDirectoryExists($destination);
152
153 42
        foreach ($this->filesystem->files($source) as $file) {
154 42
            $this->publishFile($file, $locale, $destination, $options);
155
        }
156
157 42
        return $this->results;
158
    }
159
160
    /* -----------------------------------------------------------------
161
     |  Check Methods
162
     | -----------------------------------------------------------------
163
     */
164
165
    /**
166
     * Check if the locale is a default one (English is shipped with laravel).
167
     *
168
     * @param  string  $locale
169
     *
170
     * @return bool
171
     */
172 60
    public function isDefault(string $locale): bool
173
    {
174 60
        return $locale === 'en';
175
    }
176
177
    /**
178
     * Check if the locale is supported.
179
     *
180
     * @param  string  $key
181
     *
182
     * @return bool
183
     */
184 48
    public function isSupported(string $key): bool
185
    {
186 48
        return $this->locales->has($key);
187
    }
188
189
    /**
190
     * Check if the locale is supported or fail.
191
     *
192
     * @param  string  $locale
193
     *
194
     * @throws \Arcanedev\LaravelLang\Exceptions\LangPublishException
195
     */
196 48
    private function checkLocale(string $locale): void
197
    {
198 48
        if ( ! $this->isSupported($locale)) {
199 6
            throw new LangPublishException("The locale [$locale] is not supported.");
200
        }
201 42
    }
202
203
    /* -----------------------------------------------------------------
204
     |  Other Methods
205
     | -----------------------------------------------------------------
206
     */
207
208
    /**
209
     * Publish the translation file.
210
     *
211
     * @param  SplFileInfo  $file
212
     * @param  string       $locale
213
     * @param  string       $destination
214
     * @param  array        $options
215
     */
216 42
    private function publishFile(SplFileInfo $file, string $locale, string $destination, array $options): void
217
    {
218 42
        $isInlineFile = Str::endsWith($file->getFilename(), '-inline.php');
219 42
        $destFile = $isInlineFile
220 7
            ? Str::replaceLast('-inline.php', '.php', $file->getFilename())
221 42
            : $file->getFilename();
222
223 42
        if ($this->isInResults($key = "{$locale}/{$destFile}"))
224 1
            return;
225
226
        // Ignore if inline option is not enabled
227 42
        if ($isInlineFile && (($options['inline'] ?? false) === false))
228 6
            return;
229
230
        // Ignore if force option is not enabled
231 42
        if ($this->filesystem->exists($destination.DIRECTORY_SEPARATOR.$destFile) && ($options['force'] ?? false) === false) {
232 12
            $this->results['skipped'][] = $key;
233 12
            return;
234
        }
235
236 42
        $this->filesystem->copy($file->getRealPath(), $destination.DIRECTORY_SEPARATOR.$destFile);
237 42
        $this->results['published'][] = $key;
238 42
    }
239
240
    /**
241
     * Reset the publish results.
242
     */
243 54
    private function resetResults(): void
244
    {
245 54
        $this->results = [
246
            'published' => [],
247
            'skipped'   => [],
248
        ];
249 54
    }
250
251
    /**
252
     * Check if the given key exists in results.
253
     *
254
     * @param  string  $key
255
     *
256
     * @return bool
257
     */
258 42
    private function isInResults(string $key): bool
259
    {
260 42
        return in_array($key, $this->results['published'])
261 42
            || in_array($key, $this->results['skipped']);
262
    }
263
}
264