• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OndraM / simple-google-reader / 12465171571

23 Dec 2024 10:18AM UTC coverage: 92.308%. Remained the same
12465171571

push

github

OndraM
Style: Fix non-implicit nullable parameter declaration, deprecated in PHP 8.4

60 of 65 relevant lines covered (92.31%)

8.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/src/Spreadsheets/SpreadsheetsReader.php
1
<?php
2

3
namespace OndraM\SimpleGoogleReader\Spreadsheets;
4

5
use Cocur\Slugify\Slugify;
6
use Google\Client;
7
use Google\Service\Sheets;
8
use Psr\SimpleCache\CacheInterface;
9

10
class SpreadsheetsReader
11
{
12
    private const DEFAULT_TTL = 3600;
13

14
    public function __construct(protected Client $googleClient, private Slugify $slugify, private CacheInterface $cache)
15
    {
16
    }
15✔
17

18
    /**
19
     * @return array<string, mixed>
20
     */
21
    public function readById(string $spreadsheetId, ?string $sheetName = null, int $ttl = self::DEFAULT_TTL): array
22
    {
23
        $cacheKey = $this->generateCacheKey($spreadsheetId, $sheetName);
15✔
24
        if ($this->cache->has($cacheKey)) {
15✔
25
            /** @var array<string, mixed> */
26
            return (array) $this->cache->get($cacheKey, []);
×
27
        }
28

29
        $sheetService = $this->createService();
15✔
30
        $range = 'A:Z';
15✔
31
        if ($sheetName !== null) {
15✔
32
            $range = $sheetName . '!' . $range;
10✔
33
        }
34

35
        $response = $sheetService->spreadsheets_values->get($spreadsheetId, $range);
15✔
36
        $rows = $response->getValues();
15✔
37
        if (!is_array($rows) || count($rows) === 0) {
15✔
38
            return [];
5✔
39
        }
40

41
        $header = array_map(
10✔
42
            fn ($value) => $this->slugify->slugify($value, ['separator' => '_']),
10✔
43
            array_shift($rows)
10✔
44
        );
10✔
45

46
        // Map rows to associative arrays based on header
47
        $data = array_map(
10✔
48
            function ($value) use ($header) {
10✔
49
                return array_combine($header, array_pad($value, count($header), null));
10✔
50
            },
10✔
51
            $rows
10✔
52
        );
10✔
53

54
        // Filter out rows where all values are null
55
        $data = array_filter(
10✔
56
            $data,
10✔
57
            fn($row) => count(array_filter($row, fn($value) => $value !== null)) > 0
10✔
58
        );
10✔
59

60
        $this->cache->set($cacheKey, $data, $ttl);
10✔
61

62
        return $data;
10✔
63
    }
64

65
    private function generateCacheKey(string $spreadsheetId, ?string $sheetName = null): string
66
    {
67
        return 'spreadsheet_'
15✔
68
            . sha1(
15✔
69
                $spreadsheetId
15✔
70
                . ($sheetName !== null ? '_' . $sheetName : '')
15✔
71
            );
15✔
72
    }
73

74
    private function createService(): Sheets
75
    {
76
        return new Sheets($this->googleClient);
15✔
77
    }
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc