Completed
Push — master ( d9514b...385d29 )
by Nassif
09:34
created

Api   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 2.3%

Importance

Changes 0
Metric Value
wmc 31
lcom 2
cbo 9
dl 0
loc 317
ccs 4
cts 174
cp 0.023
rs 9.8
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setSpreadsheetId() 0 6 1
A addBatchRequests() 0 9 3
A sendBatchRequests() 0 15 1
A frozenRowRequest() 0 14 1
A frozenColumnRequest() 0 14 1
A styleArea() 0 22 1
A fixedColumnWidthRequest() 0 17 1
A setSheetPropertiesRequest() 0 13 1
A addSheetRequest() 0 16 2
A clearSheetRequest() 0 11 1
A protectRangeRequest() 0 16 1
A writeCells() 0 15 1
A readCells() 0 8 1
A getSheetRowCount() 0 6 1
A getSheet() 0 10 3
A getSheets() 0 6 1
A getSheetProtectedRanges() 0 17 4
A deleteColumnsFrom() 0 12 1
A deleteRowsFrom() 0 12 1
A deleteProtectedRange() 0 8 1
A fractalColors() 0 14 2
1
<?php
2
3
namespace Nikaia\TranslationSheet\Client;
4
5
use Google_Service_Sheets;
6
use Google_Service_Sheets_BatchUpdateSpreadsheetRequest;
7
use Google_Service_Sheets_BatchUpdateValuesRequest;
8
use Google_Service_Sheets_Request;
9
use Illuminate\Support\Collection;
10
11
class Api
12
{
13
    /** @var Collection */
14
    protected $requests;
15
16
    /** @var Client */
17
    protected $client;
18
19
    /** @var string */
20
    protected $spreadsheetId;
21
22
    /**
23
     * SheetService constructor.
24
     *
25
     * @param Client $client
26
     */
27 18
    public function __construct(Client $client)
28
    {
29 18
        $this->client = $client;
30 18
        $this->requests = new Collection([]);
31 18
    }
32
33
    /**
34
     * Set the service speadsheet ID.
35
     *
36
     * @param string $spreadsheetId
37
     * @return $this
38
     */
39
    public function setSpreadsheetId($spreadsheetId)
40
    {
41
        $this->spreadsheetId = $spreadsheetId;
42
43
        return $this;
44
    }
45
46
    public function addBatchRequests($requests)
47
    {
48
        $requests = is_array($requests) ? $requests : [$requests];
49
        foreach ($requests as $request) {
50
            $this->requests->push($request);
51
        }
52
53
        return $this;
54
    }
55
56
    public function sendBatchRequests()
57
    {
58
        $sheets = new Google_Service_Sheets($this->client);
59
60
        $sheets->spreadsheets->batchUpdate(
61
            $this->spreadsheetId,
62
            new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
63
                'requests' => $this->requests->toArray(),
64
            ])
65
        );
66
67
        $this->requests = new Collection([]);
68
69
        return $this;
70
    }
71
72
    public function frozenRowRequest($sheetId, $frozonRowCount = 1)
73
    {
74
        return new Google_Service_Sheets_Request([
75
            'updateSheetProperties' => [
76
                'properties' => [
77
                    'sheetId' => $sheetId,
78
                    'gridProperties' => [
79
                        'frozenRowCount' => $frozonRowCount,
80
                    ],
81
                ],
82
                'fields' => 'gridProperties.frozenRowCount',
83
            ],
84
        ]);
85
    }
86
87
    public function frozenColumnRequest($sheetId, $frozonColumnCount = 1)
88
    {
89
        return new Google_Service_Sheets_Request([
90
            'updateSheetProperties' => [
91
                'properties' => [
92
                    'sheetId' => $sheetId,
93
                    'gridProperties' => [
94
                        'frozenColumnCount' => $frozonColumnCount,
95
                    ],
96
                ],
97
                'fields' => 'gridProperties.frozenColumnCount',
98
            ],
99
        ]);
100
    }
101
102
    public function styleArea($range, $styles)
103
    {
104
        return new Google_Service_Sheets_Request([
105
            'repeatCell' => [
106
                'range' => $range,
107
                'cell' => [
108
                    'userEnteredFormat' => [
109
                        'backgroundColor' => $this->fractalColors($styles['backgroundColor']),
110
                        'horizontalAlignment' => $styles['horizontalAlignment'],
111
                        'verticalAlignment' => $styles['verticalAlignment'],
112
                        'textFormat' => [
113
                            'foregroundColor' => $this->fractalColors($styles['foregroundColor']),
114
                            'fontSize' => $styles['fontSize'],
115
                            'fontFamily' => $styles['fontFamily'],
116
                            'bold' => $styles['bold'],
117
                        ],
118
                    ],
119
                ],
120
                'fields' => 'userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)',
121
            ],
122
        ]);
123
    }
124
125
    public function fixedColumnWidthRequest($sheetId, $startIndex, $endIndex, $width)
126
    {
127
        return new Google_Service_Sheets_Request([
128
            'updateDimensionProperties' => [
129
                'range' => [
130
                    'sheetId' => $sheetId,
131
                    'dimension' => 'COLUMNS',
132
                    'startIndex' => $startIndex,
133
                    'endIndex' => $endIndex,
134
                ],
135
                'properties' => [
136
                    'pixelSize' => $width,
137
                ],
138
                'fields' => 'pixelSize',
139
            ],
140
        ]);
141
    }
142
143
    public function setSheetPropertiesRequest($sheetId, $title, $tabColor)
144
    {
145
        return new Google_Service_Sheets_Request([
146
            'updateSheetProperties' => [
147
                'properties' => [
148
                    'sheetId' => $sheetId,
149
                    'title' => $title,
150
                    'tabColor' => $this->fractalColors($tabColor),
151
                ],
152
                'fields' => 'title,tabColor',
153
            ],
154
        ]);
155
    }
156
157
    public function addSheetRequest($title, $rowCount, $columnCount, $tabColor = null)
158
    {
159
        $properties = [
160
            'title' => $title,
161
            'gridProperties' => [
162
                'rowCount' => $rowCount,
163
                'columnCount' => $columnCount,
164
            ],
165
        ];
166
167
        if (! is_null($tabColor)) {
168
            $properties['tabColor'] = $this->fractalColors($tabColor);
169
        }
170
171
        return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]);
172
    }
173
174
    public function clearSheetRequest($sheetId)
175
    {
176
        return new Google_Service_Sheets_Request([
177
            'updateCells' => [
178
                'range' => [
179
                    'sheetId' => $sheetId,
180
                ],
181
                'fields' => 'userEnteredValue',
182
            ],
183
        ]);
184
    }
185
186
    public function protectRangeRequest($range, $description)
187
    {
188
        return new Google_Service_Sheets_Request([
189
            'addProtectedRange' => [
190
                'protectedRange' => [
191
                    'range' => $range,
192
                    'description' => $description,
193
                    'warningOnly' => false,
194
                    'editors' => [
195
                        'users' => [config('translation_sheet.serviceAccountEmail')],
196
                    ],
197
                ],
198
            ],
199
200
        ]);
201
    }
202
203
    public function writeCells($shortRange, $values)
204
    {
205
        $sheets = new \Google_Service_Sheets($this->client);
206
207
        $request = new Google_Service_Sheets_BatchUpdateValuesRequest([
208
            'valueInputOption' => 'RAW',
209
            'data' => [
210
                'range' => $shortRange,
211
                'majorDimension' => 'ROWS',
212
                'values' => $values,
213
            ],
214
        ]);
215
216
        $sheets->spreadsheets_values->batchUpdate($this->spreadsheetId, $request);
217
    }
218
219
    public function readCells($sheetId, $range)
220
    {
221
        $range .= $this->getSheetRowCount($sheetId);
222
223
        $sheets = new \Google_Service_Sheets($this->client);
224
225
        return $sheets->spreadsheets_values->get($this->spreadsheetId, $range)->values;
226
    }
227
228
    public function getSheetRowCount($sheetId)
229
    {
230
        $sheet = $this->getSheet($sheetId);
231
232
        return $sheet['modelData']['properties']['gridProperties']['rowCount'];
233
    }
234
235
    public function getSheet($sheetId)
236
    {
237
        $sheets = $this->getSheets();
238
239
        foreach ($sheets as $sheet) {
240
            if ($sheet['modelData']['properties']['sheetId'] === $sheetId) {
241
                return $sheet;
242
            }
243
        }
244
    }
245
246
    public function getSheets()
247
    {
248
        $service = new \Google_Service_Sheets($this->client);
249
250
        return $service->spreadsheets->get($this->spreadsheetId)->getSheets();
251
    }
252
253
    public function getSheetProtectedRanges($sheetId, $description = null)
254
    {
255
        $sheet = $this->getSheet($sheetId);
256
257
        if (is_null($description)) {
258
            return $sheet['protectedRanges'];
259
        }
260
261
        $ranges = [];
262
        foreach ($sheet['protectedRanges'] as $range) {
263
            if ($range->description === $description) {
264
                $ranges[] = $range;
265
            }
266
        }
267
268
        return $ranges;
269
    }
270
271
    public function deleteColumnsFrom($sheetId, $fromColumnIndex)
272
    {
273
        return new Google_Service_Sheets_Request([
274
            'deleteDimension' => [
275
                'range' => [
276
                    'sheetId' => $sheetId,
277
                    'dimension' => 'COLUMNS',
278
                    'startIndex' => $fromColumnIndex,
279
                ],
280
            ],
281
        ]);
282
    }
283
284
    public function deleteRowsFrom($sheetId, $fromRowIndex)
285
    {
286
        return new Google_Service_Sheets_Request([
287
            'deleteDimension' => [
288
                'range' => [
289
                    'sheetId' => $sheetId,
290
                    'dimension' => 'ROWS',
291
                    'startIndex' => $fromRowIndex,
292
                ],
293
            ],
294
        ]);
295
    }
296
297
    public function deleteProtectedRange($protectedRangeId)
298
    {
299
        return new Google_Service_Sheets_Request([
300
            'deleteProtectedRange' => [
301
                'protectedRangeId' => $protectedRangeId,
302
            ],
303
        ]);
304
    }
305
306
    /**
307
     * Return Fractal RGB color array with r,g,b between 0 and 1.
308
     *
309
     * @param mixed $color array of RGB color ([255, 255, 255]) or hex string (#FFFFFF)
310
     *
311
     * @return array
312
     */
313
    protected function fractalColors($color)
314
    {
315
        if (is_array($color)) {
316
            list($red, $green, $blue) = $color;
317
        } else {
318
            list($red, $green, $blue) = sscanf($color, '#%02x%02x%02x');
319
        }
320
321
        return [
322
            'red' => round($red / 255, 1),
323
            'green' => round($green / 255, 1),
324
            'blue' => round($blue / 255, 1),
325
        ];
326
    }
327
}
328