Completed
Push — master ( 318af2...9cd922 )
by Nassif
08:56
created

SheetPusher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Nikaia\TranslationSheet;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Nikaia\TranslationSheet\Commands\Output;
8
use Nikaia\TranslationSheet\Sheet\TranslationsSheet;
9
use Nikaia\TranslationSheet\Translation\Reader;
10
use Nikaia\TranslationSheet\Translation\Transformer;
11
12
class SheetPusher
13
{
14
    use Output;
15
16
    /** @var Reader */
17
    protected $reader;
18
19
    /** @var TranslationsSheet */
20
    protected $translationsSheet;
21
22
    /** @var Transformer */
23
    protected $transformer;
24
25
    public function __construct(Reader $reader, Transformer $transformer)
26
    {
27
        $this->reader = $reader;
28
        $this->transformer = $transformer;
29
30
        $this->nullOutput();
31
    }
32
33
    public function setTranslationsSheet(TranslationsSheet $translationsSheet)
34
    {
35
        $this->translationsSheet = $translationsSheet;
36
37
        $this->reader->setTranslationsSheet($translationsSheet);
38
        $this->transformer->setTranslationsSheet($translationsSheet);
39
40
        return $this;
41
    }
42
43
    public function push()
44
    {
45
        $this->output->writeln("Working on translation sheet [<comment>{$this->translationsSheet->getTitle()}</comment>] :");
46
47
        $this->output->writeln('    <comment>Scanning languages files</comment>');
48
        $translations = $this->getScannedAndTransformedTranslations();
49
        
50
51
        $this->output->writeln('    <comment>Preparing spreadsheet for new write operation</comment>');
52
        $this->translationsSheet->prepareForWrite();
53
54
        $this->output->writeln('    <comment>Updating header</comment>');
55
        $this->translationsSheet->updateHeaderRow();
56
57
        $this->output->writeln('    <comment>Writing translations in the spreadsheet</comment>');
58
        $this->translationsSheet->writeTranslations($translations->all());
59
60
        $this->output->writeln('    <comment>Styling document</comment>');
61
        $this->translationsSheet->styleDocument();
62
63
        $this->output->writeln('<info>Done</info>.');
64
        $this->output->writeln(PHP_EOL);
65
    }
66
67
    public function getScannedAndTransformedTranslations()
68
    {
69
        $excludePatterns = config('translation_sheet.exclude');
70
71
        return $this->transformer
72
            ->setLocales($this->translationsSheet->getSpreadsheet()->getLocales())
73
            ->transform($this->reader->scan())
74
            ->when(is_array($excludePatterns) && !empty($excludePatterns), function (Collection $collection) use ($excludePatterns) {
75
                return $collection->reject(function ($item) use ($excludePatterns) {
76
                    return Str::is($excludePatterns, $item[0] /* full key */);
77
                })->values();
78
            });
79
    }
80
}
81