Exporter::export()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
3
namespace tomzx\GoogleDocsToMarkdown;
4
5
use Google_Client;
6
use Google_Service_Drive;
7
use RuntimeException;
8
9
class Exporter {
10
	/**
11
	 * @var \Google_Client
12
	 */
13
	protected $client;
14
	/**
15
	 * @var \Google_Service_Drive
16
	 */
17
	protected $service;
18
	/**
19
	 * @var \tomzx\GoogleDocsToMarkdown\Converter
20
	 */
21
	protected $converter;
22
23
	/**
24
	 * @param \Google_Client $client
25
	 */
26
	public function __construct(Google_Client $client)
27
	{
28
		$this->client = $client;
29
		$this->service = new Google_Service_Drive($this->client);
30
		$this->converter = new Converter();
31
	}
32
33
	/**
34
	 * @param string $folderId
35
	 * @param $outputDirectory
36
	 * @param string $path
37
	 */
38
	public function export($folderId, $outputDirectory, $path = null)
39
	{
40
		$this->exportFiles($folderId, $outputDirectory, $path);
41
42
		$this->exportFolders($folderId, $outputDirectory, $path);
43
	}
44
45
	/**
46
	 * @param string $folderId
47
	 * @param $outputDirectory
48
	 * @param string $path
49
	 * @return array
50
	 */
51
	protected function exportFiles($folderId, $outputDirectory, $path)
52
	{
53
		$targetDirectory = $outputDirectory . '/' . $path;
54
		if ( ! file_exists($targetDirectory)) {
55
			mkdir($targetDirectory, 0777, true);
56
		}
57
58
		$parameters = [
59
			'q' => 'mimeType="application/vnd.google-apps.document"',
60
		];
61
62
		$children = $this->service->children->listChildren($folderId, $parameters);
63
		foreach ($children->getItems() as $child) {
64
			$fileId = $child->getId();
65
			$file = $this->service->files->get($fileId);
66
			$title = $file->getTitle();
67
			$targetPath = $path ? $path . '/' . $title : $title;
68
69
			echo 'Downloading ' . $targetPath . ' ... ';
70
71
			$downloadUrl = $file->getExportLinks()['text/html'];
72
			$content = file_get_contents($downloadUrl);
73
74
			if ($content === false) {
75
				throw new RuntimeException('There was an error while fetching the content of ' . $downloadUrl);
76
			}
77
78
			echo 'Converting ... ';
79
80
			$markdown = $this->converter->convert($content);
81
82
			$filename = $title . '.md';
83
			file_put_contents($targetDirectory . '/' . $filename, $markdown);
84
85
			echo 'Done!' . PHP_EOL;
86
		}
87
	}
88
89
	/**
90
	 * @param string $folderId
91
	 * @param $outputDirectory
92
	 * @param string $path
93
	 */
94
	protected function exportFolders($folderId, $outputDirectory, $path)
95
	{
96
		$parameters = [
97
			'q' => 'mimeType="application/vnd.google-apps.folder"',
98
		];
99
100
		$children = $this->service->children->listChildren($folderId, $parameters);
101
		foreach ($children->getItems() as $child) {
102
			$folderId = $child->getId();
103
			$folder = $this->service->files->get($folderId);
104
			$title = $folder->getTitle();
105
106
			$path = $path ? $path . '/' . $title : $title;
107
			$this->export($folderId, $outputDirectory, $path);
108
		}
109
	}
110
}
111