Completed
Push — master ( 4b1bca...1fe37b )
by Nassif
34:03
created

TranslationsSheet   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.41%

Importance

Changes 5
Bugs 4 Features 0
Metric Value
wmc 19
c 5
b 4
f 0
lcom 1
cbo 5
dl 0
loc 153
ccs 85
cts 91
cp 0.9341
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getTitle() 0 4 1
A coordinates() 0 4 1
A emptyCoordinates() 0 4 1
A setup() 0 9 1
A writeTranslations() 0 7 1
A readTranslations() 0 4 1
B styleDocument() 0 44 2
A prepareForWrite() 0 6 1
A lockTranslations() 0 11 1
A unlockTranslations() 0 14 2
A isTranslationsLocked() 0 6 2
A removeAllProtectedRanges() 0 13 3
A updateHeaderRow() 0 7 1
1
<?php
2
3
namespace Nikaia\TranslationSheet\Sheet;
4
5
class TranslationsSheet extends AbstractSheet
6
{
7 10
    public function getId()
8
    {
9 10
        return 0;
10
    }
11
12 8
    public function getTitle()
13
    {
14 8
        return 'Translations';
15
    }
16
17 6
    public function coordinates()
18
    {
19 6
        return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle());
20
    }
21
22 2
    public function emptyCoordinates()
23
    {
24 2
        return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle());
25
    }
26
27 1
    public function setup()
28
    {
29 1
        $this->spreadsheet->api()
30 1
            ->addBatchRequests([
31
                // Set properties
32 1
                $this->spreadsheet->api()->setSheetPropertiesRequest($this->getId(), $this->getTitle(), $this->styles()->translationSheetTabColor()),
33 1
            ])
34 1
            ->sendBatchRequests();
35 1
    }
36
37 1
    public function writeTranslations($translations)
38
    {
39 1
        $this->spreadsheet->setTranslations($translations);
40
41 1
        $this->spreadsheet->api()
42 1
            ->writeCells($this->coordinates()->dataShortRange(), $translations);
43 1
    }
44
45 1
    public function readTranslations()
46
    {
47 1
        return $this->spreadsheet->api()->readCells($this->getId(), $this->coordinates()->dataShortRange(2, true));
48
    }
49
50 1
    public function styleDocument()
51
    {
52 1
        $fullkeyRange = $this->coordinates()->fullKeyColumnRange();
53 1
        $translationsRange = $this->coordinates()->translationsRange();
54 1
        $metaRange = $this->coordinates()->metaColumnsRange();
55
56
        $requests = [
57
            // Header row
58 1
            $this->spreadsheet->api()->frozenRowRequest($this->getId()),
59 1
            $this->spreadsheet->api()->styleArea($this->emptyCoordinates()->headerRange(), $this->styles()->translationsHeader()),
60 1
            $this->spreadsheet->api()->protectRangeRequest($this->emptyCoordinates()->headerRange(), 'HEADER.'),
61
62
            // Full key column
63 1
            $this->spreadsheet->api()->frozenColumnRequest($this->getId()),
64 1
            $this->spreadsheet->api()->protectRangeRequest($fullkeyRange, 'FULL_KEY'),
65 1
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), 0, 1, 450),
66 1
            $this->spreadsheet->api()->styleArea($fullkeyRange, $this->styles()->fullKeyColumn()),
67
68
            // Translations columns
69 1
            $this->spreadsheet->api()->styleArea($translationsRange, $this->styles()->translationsColumns()),
70
71
            // Meta columns
72 1
            $this->spreadsheet->api()->protectRangeRequest($metaRange, 'META'),
73 1
            $this->spreadsheet->api()->styleArea($metaRange, $this->styles()->metaColumns()),
74 1
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->namespaceColumnIndex(), $this->coordinates()->namespaceColumnIndex() + 1, 145),
75 1
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->groupColumnIndex(), $this->coordinates()->groupColumnIndex() + 1, 80),
76 1
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->keyColumnIndex(), $this->coordinates()->keyColumnIndex() + 1, 240),
77 1
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->sourceFileColumnIndex(), $this->coordinates()->sourceFileColumnIndex() + 1, 360),
78
79
            // Delete extra columns and rows
80 1
            $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()),
81 1
            $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()),
82 1
        ];
83
84
        // Fixed locales translations column width
85 1
        $beginAt = 1;
86 1
        foreach ($this->spreadsheet->getLocales() as $locale) {
87 1
            $requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350);
88 1
            $beginAt++;
89 1
        }
90
91
        // Send requests
92 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
93 1
    }
94
95 1
    public function prepareForWrite()
96
    {
97
        // We need to remove all protected ranges, to avoid any duplicates that may lead to
98
        // some weird behaviours
99 1
        $this->removeAllProtectedRanges();
100 1
    }
101
102 1
    public function lockTranslations()
103
    {
104 1
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
105
106 1
        $this->api()
107 1
            ->addBatchRequests([
108 1
                $this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'),
109 1
                $this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()),
110 1
            ])
111 1
            ->sendBatchRequests();
112 1
    }
113
114 1
    public function unlockTranslations()
115
    {
116 1
        $requests = [];
117 1
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
118
119 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
120 1
        foreach ($protectedRanges as $protectedRange) {
121 1
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
122 1
        }
123
124 1
        $requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns());
125
126 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
127 1
    }
128
129 1
    public function isTranslationsLocked()
130
    {
131 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
132
133 1
        return ! empty($protectedRanges) && count($protectedRanges) > 0;
134
    }
135
136 1
    public function removeAllProtectedRanges()
137
    {
138 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId());
139
140 1
        $requests = [];
141 1
        foreach ($protectedRanges as $protectedRange) {
142 1
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
143 1
        }
144
145 1
        if (! empty($requests)) {
146 1
            $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
147 1
        }
148 1
    }
149
150
    public function updateHeaderRow()
151
    {
152
        $this->api()->writeCells(
153
            $this->emptyCoordinates()->headerShortRange(),
154
            [$this->spreadsheet->getHeader()]
155
        );
156
    }
157
}
158