Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

Api   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 3.17%

Importance

Changes 0
Metric Value
wmc 31
lcom 2
cbo 9
dl 0
loc 316
ccs 4
cts 126
cp 0.0317
rs 9.92
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 writeCells() 0 14 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
A protectRangeRequest() 0 16 1
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)
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
        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)
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
        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)
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
        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
        $request->setValueInputOption('RAW');
209
        $request->setData([
210
            'range' => $shortRange,
211
            'majorDimension' => 'ROWS',
212
            'values' => $values,
213
        ]);
214
215
        $sheets->spreadsheets_values->batchUpdate($this->spreadsheetId, $request);
216
    }
217
218
    public function readCells($sheetId, $range)
219
    {
220
        $range .= $this->getSheetRowCount($sheetId);
221
222
        $sheets = new \Google_Service_Sheets($this->client);
223
224
        return $sheets->spreadsheets_values->get($this->spreadsheetId, $range)->values;
225
    }
226
227
    public function getSheetRowCount($sheetId)
228
    {
229
        $sheet = $this->getSheet($sheetId);
230
231
        return $sheet['properties']['gridProperties']['rowCount'];
232
    }
233
234
    public function getSheet($sheetId)
235
    {
236
        $sheets = $this->getSheets();
237
238
        foreach ($sheets as $sheet) {
239
            if ($sheet['properties']['sheetId'] === $sheetId) {
240
                return $sheet;
241
            }
242
        }
243
    }
244
245
    public function getSheets()
246
    {
247
        $service = new \Google_Service_Sheets($this->client);
248
249
        return $service->spreadsheets->get($this->spreadsheetId)->getSheets();
250
    }
251
252
    public function getSheetProtectedRanges($sheetId, $description = null)
253
    {
254
        $sheet = $this->getSheet($sheetId);
255
256
        if (is_null($description)) {
257
            return $sheet['protectedRanges'];
258
        }
259
260
        $ranges = [];
261
        foreach ($sheet['protectedRanges'] as $range) {
262
            if ($range->description === $description) {
263
                $ranges[] = $range;
264
            }
265
        }
266
267
        return $ranges;
268
    }
269
270
    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...
271
    {
272
        return new Google_Service_Sheets_Request([
273
            'deleteDimension' => [
274
                'range' => [
275
                    'sheetId' => $sheetId,
276
                    'dimension' => 'COLUMNS',
277
                    'startIndex' => $fromColumnIndex,
278
                ],
279
            ],
280
        ]);
281
    }
282
283
    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...
284
    {
285
        return new Google_Service_Sheets_Request([
286
            'deleteDimension' => [
287
                'range' => [
288
                    'sheetId' => $sheetId,
289
                    'dimension' => 'ROWS',
290
                    'startIndex' => $fromRowIndex,
291
                ],
292
            ],
293
        ]);
294
    }
295
296
    public function deleteProtectedRange($protectedRangeId)
297
    {
298
        return new Google_Service_Sheets_Request([
299
            'deleteProtectedRange' => [
300
                'protectedRangeId' => $protectedRangeId,
301
            ],
302
        ]);
303
    }
304
305
    /**
306
     * Return Fractal RGB color array with r,g,b between 0 and 1.
307
     *
308
     * @param mixed $color array of RGB color ([255, 255, 255]) or hex string (#FFFFFF)
309
     *
310
     * @return array
311
     */
312
    protected function fractalColors($color)
313
    {
314
        if (is_array($color)) {
315
            list($red, $green, $blue) = $color;
316
        } else {
317
            list($red, $green, $blue) = sscanf($color, '#%02x%02x%02x');
318
        }
319
320
        return [
321
            'red' => round($red / 255, 1),
322
            'green' => round($green / 255, 1),
323
            'blue' => round($blue / 255, 1),
324
        ];
325
    }
326
}
327