Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

TranslationsSheet   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.02%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 160
ccs 80
cts 86
cp 0.9302
rs 10
c 0
b 0
f 0

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
A styleDocument() 0 51 3
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
            ])
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
80
        // Fixed locales translations column width
81 1
        $beginAt = 1;
82 1
        foreach ($this->spreadsheet->getLocales() as $locale) {
83 1
            $requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350);
84 1
            $beginAt++;
85
        }
86
87
        // Send requests
88 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
89
90
        // Delete extra columns and rows if any
91
        try {
92 1
            $this->spreadsheet->api()->addBatchRequests([
93 1
                $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()),
94 1
                $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()),
95 1
            ])->sendBatchRequests();
96
        } catch (\Google_Service_Exception $e) {
97
            // If there is no extra columns or rows Google api will raise an exception :
98
            // ... Invalid requests[xxx].deleteDimension: Cannot delete a column that doesn't exist ...
99
        }
100 1
    }
101
102 1
    public function prepareForWrite()
103
    {
104
        // We need to remove all protected ranges, to avoid any duplicates that may lead to
105
        // some weird behaviours
106 1
        $this->removeAllProtectedRanges();
107 1
    }
108
109 1
    public function lockTranslations()
110
    {
111 1
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
112
113 1
        $this->api()
114 1
            ->addBatchRequests([
115 1
                $this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'),
116 1
                $this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()),
117
            ])
118 1
            ->sendBatchRequests();
119 1
    }
120
121 1
    public function unlockTranslations()
122
    {
123 1
        $requests = [];
124 1
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
125
126 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
127 1
        foreach ($protectedRanges as $protectedRange) {
128 1
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
129
        }
130
131 1
        $requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns());
132
133 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
134 1
    }
135
136 1
    public function isTranslationsLocked()
137
    {
138 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
139
140 1
        return ! empty($protectedRanges) && count($protectedRanges) > 0;
141
    }
142
143 1
    public function removeAllProtectedRanges()
144
    {
145 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId());
146
147 1
        $requests = [];
148 1
        foreach ($protectedRanges as $protectedRange) {
149 1
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
150
        }
151
152 1
        if (! empty($requests)) {
153 1
            $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
154
        }
155 1
    }
156
157
    public function updateHeaderRow()
158
    {
159
        $this->api()->writeCells(
160
            $this->emptyCoordinates()->headerShortRange(),
161
            [$this->spreadsheet->getHeader()]
162
        );
163
    }
164
}
165