Completed
Push — master ( 385d29...bcac64 )
by Nassif
09:52
created

TranslationsSheet::styleDocument()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 31
cts 31
cp 1
rs 9.4109
cc 3
eloc 30
nc 4
nop 0
crap 3

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 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
80 1
        // Fixed locales translations column width
81 1
        $beginAt = 1;
82 1
        foreach ($this->spreadsheet->getLocales() as $locale) {
83
            $requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350);
84
            $beginAt++;
85 1
        }
86 1
87 1
        // Send requests
88 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
89 1
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
                $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 1
        }
100 1
    }
101
102 1
    public function prepareForWrite()
103
    {
104 1
        // 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 1
109 1
    public function lockTranslations()
110 1
    {
111 1
        $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId()));
112 1
113
        $this->api()
114 1
            ->addBatchRequests([
115
                $this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'),
116 1
                $this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()),
117 1
            ])
118
            ->sendBatchRequests();
119 1
    }
120 1
121 1
    public function unlockTranslations()
122 1
    {
123
        $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
            $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId);
129 1
        }
130
131 1
        $requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns());
132
133 1
        $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests();
134
    }
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 1
    }
142 1
143 1
    public function removeAllProtectedRanges()
144
    {
145 1
        $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId());
146 1
147 1
        $requests = [];
148 1
        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