Completed
Push — master ( bcac64...9b6ee4 )
by Nassif
11:39
created

TranslationsSheet   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 9.3%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 160
ccs 8
cts 86
cp 0.093
rs 10
c 2
b 1
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 2
    public function getId()
8
    {
9 2
        return 0;
10
    }
11
12 2
    public function getTitle()
13
    {
14 2
        return 'Translations';
15
    }
16
17 1
    public function coordinates()
18
    {
19 1
        return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle());
20
    }
21
22 1
    public function emptyCoordinates()
23
    {
24 1
        return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle());
25
    }
26
27
    public function setup()
28
    {
29
        $this->spreadsheet->api()
30
            ->addBatchRequests([
31
                // Set properties
32
                $this->spreadsheet->api()->setSheetPropertiesRequest($this->getId(), $this->getTitle(), $this->styles()->translationSheetTabColor()),
33
            ])
34
            ->sendBatchRequests();
35
    }
36
37
    public function writeTranslations($translations)
38
    {
39
        $this->spreadsheet->setTranslations($translations);
40
41
        $this->spreadsheet->api()
42
            ->writeCells($this->coordinates()->dataShortRange(), $translations);
43
    }
44
45
    public function readTranslations()
46
    {
47
        return $this->spreadsheet->api()->readCells($this->getId(), $this->coordinates()->dataShortRange(2, true));
48
    }
49
50
    public function styleDocument()
51
    {
52
        $fullkeyRange = $this->coordinates()->fullKeyColumnRange();
53
        $translationsRange = $this->coordinates()->translationsRange();
54
        $metaRange = $this->coordinates()->metaColumnsRange();
55
56
        $requests = [
57
            // Header row
58
            $this->spreadsheet->api()->frozenRowRequest($this->getId()),
59
            $this->spreadsheet->api()->styleArea($this->emptyCoordinates()->headerRange(), $this->styles()->translationsHeader()),
60
            $this->spreadsheet->api()->protectRangeRequest($this->emptyCoordinates()->headerRange(), 'HEADER.'),
61
62
            // Full key column
63
            $this->spreadsheet->api()->frozenColumnRequest($this->getId()),
64
            $this->spreadsheet->api()->protectRangeRequest($fullkeyRange, 'FULL_KEY'),
65
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), 0, 1, 450),
66
            $this->spreadsheet->api()->styleArea($fullkeyRange, $this->styles()->fullKeyColumn()),
67
68
            // Translations columns
69
            $this->spreadsheet->api()->styleArea($translationsRange, $this->styles()->translationsColumns()),
70
71
            // Meta columns
72
            $this->spreadsheet->api()->protectRangeRequest($metaRange, 'META'),
73
            $this->spreadsheet->api()->styleArea($metaRange, $this->styles()->metaColumns()),
74
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->namespaceColumnIndex(), $this->coordinates()->namespaceColumnIndex() + 1, 145),
75
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->groupColumnIndex(), $this->coordinates()->groupColumnIndex() + 1, 80),
76
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->keyColumnIndex(), $this->coordinates()->keyColumnIndex() + 1, 240),
77
            $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->sourceFileColumnIndex(), $this->coordinates()->sourceFileColumnIndex() + 1, 360),
78
        ];
79
80
        // Fixed locales translations column width
81
        $beginAt = 1;
82
        foreach ($this->spreadsheet->getLocales() as $locale) {
83
            $requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350);
84
            $beginAt++;
85
        }
86
87
        // Send requests
88
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
89
90
        // Delete extra columns and rows if any
91
        try {
92
            $this->spreadsheet->api()->addBatchRequests([
93
                $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()),
94
                $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()),
95
            ])->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
    }
101
102
    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
        $this->removeAllProtectedRanges();
107
    }
108
109
    public function lockTranslations()
110
    {
111
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
112
113
        $this->api()
114
            ->addBatchRequests([
115
                $this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'),
116
                $this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()),
117
            ])
118
            ->sendBatchRequests();
119
    }
120
121
    public function unlockTranslations()
122
    {
123
        $requests = [];
124
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
125
126
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
127
        foreach ($protectedRanges as $protectedRange) {
128
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
129
        }
130
131
        $requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns());
132
133
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
134
    }
135
136
    public function isTranslationsLocked()
137
    {
138
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS');
139
140
        return !empty($protectedRanges) && count($protectedRanges) > 0;
141
    }
142
143
    public function removeAllProtectedRanges()
144
    {
145
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId());
146
147
        $requests = [];
148
        foreach ($protectedRanges as $protectedRange) {
149
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
150
        }
151
152
        if (!empty($requests)) {
153
            $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
154
        }
155
    }
156
157
    public function updateHeaderRow()
158
    {
159
        $this->api()->writeCells(
160
            $this->emptyCoordinates()->headerShortRange(),
161
            [$this->spreadsheet->getHeader()]
162
        );
163
    }
164
}
165