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

OndraM / simple-google-reader / 12523194269

23 Dec 2024 11:17AM UTC coverage: 92.424%. Remained the same
12523194269

push

github

OndraM
Chore(release): Release version 1.0.1

61 of 66 relevant lines covered (92.42%)

8.48 hits per line

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

87.1
/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
class DocsReader
11
{
12
    private const DEFAULT_TTL = 3600;
13

14
    public function __construct(private readonly Client $googleClient, private readonly CacheInterface $cache)
15
    {
16
    }
10✔
17

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

31
            return is_string($cacheData) ? $cacheData : '';
×
32
        }
33

34
        $documentService = $this->createDocsService();
5✔
35

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

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

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

52
        $this->cache->set($cacheKey, $bodyText, $ttl);
5✔
53

54
        return $bodyText;
5✔
55
    }
56

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

69
            return is_string($cacheData) ? $cacheData : '';
×
70
        }
71

72
        $driveService = $this->createDriveService();
5✔
73

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

79
        $this->cache->set($cacheKey, $htmlContent, $ttl);
5✔
80

81
        return $htmlContent;
5✔
82
    }
83

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

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

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