Completed
Push — master ( 9ddd67...e7eb1b )
by Nassif
01:49
created

TranslationsSheet::styleDocument()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 3.0003

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 28
cts 29
cp 0.9655
rs 9.069
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.0003

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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