Completed
Push — master ( 9a8731...f9436b )
by Nassif
03:22
created

Api::addBlankSheet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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 19
    public function __construct(Client $client)
28
    {
29 19
        $this->client = $client;
30 19
        $this->requests = new Collection([]);
31 19
    }
32
33
    /**
34
     * Set the service speadsheet ID.
35
     *
36
     * @param string $spreadsheetId
37
     * @return $this
38
     */
39 2
    public function setSpreadsheetId($spreadsheetId)
40
    {
41 2
        $this->spreadsheetId = $spreadsheetId;
42
43 2
        return $this;
44
    }
45
46 1
    public function addBatchRequests($requests)
47
    {
48 1
        $requests = is_array($requests) ? $requests : [$requests];
49 1
        foreach ($requests as $request) {
50 1
            $this->requests->push($request);
51
        }
52
53 1
        return $this;
54
    }
55
56 1
    public function sendBatchRequests()
57
    {
58 1
        $sheets = new Google_Service_Sheets($this->client);
59
60 1
        $sheets->spreadsheets->batchUpdate(
61 1
            $this->spreadsheetId,
62 1
            new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
63 1
                'requests' => $this->requests->toArray(),
64
            ])
65
        );
66
67 1
        $this->requests = new Collection([]);
68
69 1
        return $this;
70
    }
71
72 1
    public function frozenRowRequest($sheetId, $frozonRowCount = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74 1
        return new Google_Service_Sheets_Request([
75
            'updateSheetProperties' => [
76
                'properties' => [
77 1
                    'sheetId' => $sheetId,
78
                    'gridProperties' => [
79 1
                        'frozenRowCount' => $frozonRowCount,
80
                    ],
81
                ],
82 1
                'fields' => 'gridProperties.frozenRowCount',
83
            ],
84
        ]);
85
    }
86
87 1
    public function frozenColumnRequest($sheetId, $frozonColumnCount = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89 1
        return new Google_Service_Sheets_Request([
90
            'updateSheetProperties' => [
91
                'properties' => [
92 1
                    'sheetId' => $sheetId,
93
                    'gridProperties' => [
94 1
                        'frozenColumnCount' => $frozonColumnCount,
95
                    ],
96
                ],
97 1
                'fields' => 'gridProperties.frozenColumnCount',
98
            ],
99
        ]);
100
    }
101
102 1
    public function styleArea($range, $styles)
103
    {
104 1
        return new Google_Service_Sheets_Request([
105
            'repeatCell' => [
106 1
                'range' => $range,
107
                'cell' => [
108
                    'userEnteredFormat' => [
109 1
                        'backgroundColor' => $this->fractalColors($styles['backgroundColor']),
110 1
                        'horizontalAlignment' => $styles['horizontalAlignment'],
111 1
                        'verticalAlignment' => $styles['verticalAlignment'],
112
                        'textFormat' => [
113 1
                            'foregroundColor' => $this->fractalColors($styles['foregroundColor']),
114 1
                            'fontSize' => $styles['fontSize'],
115 1
                            'fontFamily' => $styles['fontFamily'],
116 1
                            'bold' => $styles['bold'],
117
                        ],
118
                    ],
119
                ],
120 1
                'fields' => 'userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)',
121
            ],
122
        ]);
123
    }
124
125 1
    public function fixedColumnWidthRequest($sheetId, $startIndex, $endIndex, $width)
126
    {
127 1
        return new Google_Service_Sheets_Request([
128
            'updateDimensionProperties' => [
129
                'range' => [
130 1
                    'sheetId' => $sheetId,
131 1
                    'dimension' => 'COLUMNS',
132 1
                    'startIndex' => $startIndex,
133 1
                    'endIndex' => $endIndex,
134
                ],
135
                'properties' => [
136 1
                    'pixelSize' => $width,
137
                ],
138 1
                'fields' => 'pixelSize',
139
            ],
140
        ]);
141
    }
142
143 1
    public function setSheetPropertiesRequest($sheetId, $title, $tabColor)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 1
        return new Google_Service_Sheets_Request([
146
            'updateSheetProperties' => [
147
                'properties' => [
148 1
                    'sheetId' => $sheetId,
149 1
                    'title' => $title,
150 1
                    'tabColor' => $this->fractalColors($tabColor),
151
                ],
152 1
                'fields' => 'title,tabColor',
153
            ],
154
        ]);
155
    }
156
157 1
    public function addSheetRequest($title, $rowCount, $columnCount, $tabColor = null)
158
    {
159
        $properties = [
160 1
            'title' => $title,
161
            'gridProperties' => [
162 1
                'rowCount' => $rowCount,
163 1
                'columnCount' => $columnCount,
164
            ],
165
        ];
166
167 1
        if (!is_null($tabColor)) {
168 1
            $properties['tabColor'] = $this->fractalColors($tabColor);
169
        }
170
171 1
        return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]);
172
    }
173
174 1
    public function addBlankSheet($title = null)
175
    {
176 1
        $properties = $title ? ['title' => $title] : [];
177 1
        return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]);
178
    }
179
180
    public function clearSheetRequest($sheetId)
181
    {
182
        return new Google_Service_Sheets_Request([
183
            'updateCells' => [
184
                'range' => [
185
                    'sheetId' => $sheetId,
186
                ],
187
                'fields' => 'userEnteredValue',
188
            ],
189
        ]);
190
    }
191
192 1
    public function protectRangeRequest($range, $description)
193
    {
194 1
        return new Google_Service_Sheets_Request([
195
            'addProtectedRange' => [
196
                'protectedRange' => [
197 1
                    'range' => $range,
198 1
                    'description' => $description,
199
                    'warningOnly' => false,
200
                    'editors' => [
201 1
                        'users' => [config('translation_sheet.serviceAccountEmail')],
202
                    ],
203
                ],
204
            ],
205
206
        ]);
207
    }
208
209 1
    public function writeCells($shortRange, $values)
210
    {
211 1
        $sheets = new \Google_Service_Sheets($this->client);
212
213 1
        $request = new Google_Service_Sheets_BatchUpdateValuesRequest();
214 1
        $request->setValueInputOption('RAW');
215 1
        $request->setData([
216 1
            'range' => $shortRange,
217 1
            'majorDimension' => 'ROWS',
218 1
            'values' => $values,
219
        ]);
220
221 1
        $sheets->spreadsheets_values->batchUpdate($this->spreadsheetId, $request);
222 1
    }
223
224 1
    public function readCells($sheetId, $range)
225
    {
226 1
        $range .= $this->getSheetRowCount($sheetId);
227
228 1
        $sheets = new \Google_Service_Sheets($this->client);
229
230 1
        return $sheets->spreadsheets_values->get($this->spreadsheetId, $range)->values;
231
    }
232
233 1
    public function getSheetRowCount($sheetId)
234
    {
235 1
        $sheet = $this->getSheet($sheetId);
236
237 1
        return $sheet['properties']['gridProperties']['rowCount'];
238
    }
239
240 1
    public function getSheet($sheetId)
241
    {
242 1
        $sheets = $this->getSheets();
243
244 1
        foreach ($sheets as $sheet) {
245 1
            if ($sheet['properties']['sheetId'] === $sheetId) {
246 1
                return $sheet;
247
            }
248
        }
249
    }
250
251 2
    public function getSheets()
252
    {
253 2
        $service = new \Google_Service_Sheets($this->client);
254
255 2
        return $service->spreadsheets->get($this->spreadsheetId)->getSheets();
256
    }
257
258 2
    public function firstSheetId()
259
    {
260 2
        return data_get(collect($this->getSheets())->first(), 'properties.sheetId');
261
    }
262
263 1
    public function getSheetProtectedRanges($sheetId, $description = null)
264
    {
265 1
        $sheet = $this->getSheet($sheetId);
266
267 1
        if (is_null($description)) {
268 1
            return $sheet['protectedRanges'];
269
        }
270
271
        $ranges = [];
272
        foreach ($sheet['protectedRanges'] as $range) {
273
            if ($range->description === $description) {
274
                $ranges[] = $range;
275
            }
276
        }
277
278
        return $ranges;
279
    }
280
281 1
    public function deleteColumnsFrom($sheetId, $fromColumnIndex)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
    {
283 1
        return new Google_Service_Sheets_Request([
284
            'deleteDimension' => [
285
                'range' => [
286 1
                    'sheetId' => $sheetId,
287 1
                    'dimension' => 'COLUMNS',
288 1
                    'startIndex' => $fromColumnIndex,
289
                ],
290
            ],
291
        ]);
292
    }
293
294 1
    public function deleteRowsFrom($sheetId, $fromRowIndex)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
    {
296 1
        return new Google_Service_Sheets_Request([
297
            'deleteDimension' => [
298
                'range' => [
299 1
                    'sheetId' => $sheetId,
300 1
                    'dimension' => 'ROWS',
301 1
                    'startIndex' => $fromRowIndex,
302
                ],
303
            ],
304
        ]);
305
    }
306
307
    public function deleteProtectedRange($protectedRangeId)
308
    {
309
        return new Google_Service_Sheets_Request([
310
            'deleteProtectedRange' => [
311
                'protectedRangeId' => $protectedRangeId,
312
            ],
313
        ]);
314
    }
315
316 1
    public function deleteSheetRequest($sheetId)
317
    {
318 1
        return new Google_Service_Sheets_Request([
319
            'deleteSheet' => [
320 1
                'sheetId' => $sheetId,
321
            ],
322
        ]);
323
    }
324
325
    /**
326
     * Return Fractal RGB color array with r,g,b between 0 and 1.
327
     *
328
     * @param mixed $color array of RGB color ([255, 255, 255]) or hex string (#FFFFFF)
329
     *
330
     * @return array
331
     */
332 1
    protected function fractalColors($color)
333
    {
334 1
        if (is_array($color)) {
335 1
            list($red, $green, $blue) = $color;
336
        } else {
337 1
            list($red, $green, $blue) = sscanf($color, '#%02x%02x%02x');
338
        }
339
340
        return [
341 1
            'red' => round($red / 255, 1),
342 1
            'green' => round($green / 255, 1),
343 1
            'blue' => round($blue / 255, 1),
344
        ];
345
    }
346
347
348
}
349