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

OndraM / simple-google-reader / 22279327646

22 Feb 2026 02:49PM UTC coverage: 91.935% (-0.5%) from 92.424%
22279327646

push

github

OndraM
Chore: Upgrade PHP syntax

1 of 1 new or added line in 1 file covered. (100.0%)

3 existing lines in 2 files now uncovered.

57 of 62 relevant lines covered (91.94%)

8.31 hits per line

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

86.67
/src/Docs/DocsReader.php
1
<?php
2

3
namespace OndraM\SimpleGoogleReader\Docs;
4

5
use Google\Client;
6
use Google\Service\Docs;
7
use Google\Service\Drive;
8
use Psr\SimpleCache\CacheInterface;
9

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

14
    public function __construct(private Client $googleClient, private CacheInterface $cache) {}
15

16
    /**
17
     * Read contents of the document as plaintext. Only text elements and read, other elements like tables are ignored.
18
     * All document formatting is ignored.
19
     *
20
     * You must enable Google Docs API in your Google Cloud Console project:
21
     * https://console.cloud.google.com/apis/library/docs.googleapis.com
22
     */
23
    public function readAsPlaintext(string $documentId, int $ttl = self::DEFAULT_TTL): string
24
    {
25
        $cacheKey = $this->generateCacheKey($documentId, 'text');
5✔
26
        if ($this->cache->has($cacheKey)) {
5✔
UNCOV
27
            $cacheData = $this->cache->get($cacheKey, '');
×
28

29
            return is_string($cacheData) ? $cacheData : '';
×
30
        }
31

32
        $documentService = $this->createDocsService();
5✔
33

34
        $doc = $documentService->documents->get($documentId);
5✔
35
        $bodyContent = $doc->getBody()->getContent();
5✔
36

37
        $bodyText = '';
5✔
38
        foreach ($bodyContent as $structuralElement) {
5✔
39
            if ($structuralElement->getParagraph() === null) {
5✔
40
                continue;
5✔
41
            }
42

43
            foreach ($structuralElement->getParagraph()->getElements() as $element) {
5✔
44
                if ($element->getTextRun()) {
5✔
45
                    $bodyText .= $element->getTextRun()->getContent();
5✔
46
                }
47
            }
48
        }
49

50
        $this->cache->set($cacheKey, $bodyText, $ttl);
5✔
51

52
        return $bodyText;
5✔
53
    }
54

55
    /**
56
     * Read contents of the document as HTML.
57
     *
58
     * To be able to read the doc as HTML, you must enable Google Drive API in your Google Cloud Console project:
59
     * https://console.cloud.google.com/apis/library/drive.googleapis.com
60
     */
61
    public function readAsHtml(string $documentId, int $ttl = self::DEFAULT_TTL): string
62
    {
63
        $cacheKey = $this->generateCacheKey($documentId, 'html');
5✔
64
        if ($this->cache->has($cacheKey)) {
5✔
UNCOV
65
            $cacheData = $this->cache->get($cacheKey, '');
×
66

67
            return is_string($cacheData) ? $cacheData : '';
×
68
        }
69

70
        $driveService = $this->createDriveService();
5✔
71

72
        $response = $driveService->files->export($documentId, 'text/html', [
5✔
73
            'alt' => 'media',
5✔
74
        ]);
5✔
75
        $htmlContent = $response->getBody()->getContents();
5✔
76

77
        $this->cache->set($cacheKey, $htmlContent, $ttl);
5✔
78

79
        return $htmlContent;
5✔
80
    }
81

82
    private function generateCacheKey(string $documentId, string $type): string
83
    {
84
        return 'doc_' . sha1($documentId) . '_' . $type;
10✔
85
    }
86

87
    private function createDocsService(): Docs
88
    {
89
        return new Docs($this->googleClient);
5✔
90
    }
91

92
    private function createDriveService(): Drive
93
    {
94
        return new Drive($this->googleClient);
5✔
95
    }
96
}
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

© 2026 Coveralls, Inc