Completed
Push — master ( 567a90...f50758 )
by Sebastian
02:46
created

GoogleDrive::collectBackups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
2
namespace phpbu\App\Backup\Collector;
3
4
use Google_Client;
5
use Google_Service_Drive;
6
use phpbu\App\Backup\Collector;
7
use phpbu\App\Backup\File;
8
use phpbu\App\Backup\Path;
9
use phpbu\App\Backup\Target;
10
11
/**
12
 * GoogleDrive class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 5.1.0
21
 */
22
class GoogleDrive extends Remote implements Collector
23
{
24
    /**
25
     * Google api client.
26
     *
27
     * @var \Google_Client
28
     */
29
    private $client;
30
31
    /**
32
     * Parent folder id.
33
     *
34
     * @var string
35
     */
36
    private $parent;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param \phpbu\App\Backup\Target $target
42
     * @param \phpbu\App\Backup\Path   $path
43
     * @param \Google_Client           $client
44
     */
45
    public function __construct(Target $target, Path $path, Google_Client $client)
46
    {
47
        $this->setUp($target, $path);
48
        $this->client = $client;
49
        $this->parent = $path->getPath();
50
    }
51
52
    /**
53
     * Collect all created backups.
54
     *
55
     * @return void
56
     */
57
    protected function collectBackups()
58
    {
59
        $service = new Google_Service_Drive($this->client);
60
        $results = $service->files->listFiles($this->getParams());
61
62
        /** @var \Google_Service_Drive_DriveFile $googleFile */
63
        foreach ($results->getFiles() as $googleFile) {
64
            if ($this->isFileMatch($googleFile->getName())) {
65
                $file                                    = new File\GoogleDrive($this->client, $googleFile);
66
                $this->files[$this->getFileIndex($file)] = $file;
67
            }
68
        }
69
    }
70
71
    /**
72
     * Return google api params list to find all backups.
73
     *
74
     * @return array
75
     */
76
    private function getParams() : array
77
    {
78
        return [
79
            'includeTeamDriveItems' => false,
80
            'pageSize'              => 1000,
81
            'fields'                => 'nextPageToken, files(id, name, createdTime, size)',
82
            'spaces'                => 'drive',
83
            'q'                     => 'trashed = false AND visibility = \'limited\'' . $this->getParentsFilter(),
84
        ];
85
    }
86
87
    /**
88
     * Return parent filter query.
89
     *
90
     * @return string
91
     */
92
    private function getParentsFilter() : string
93
    {
94
        return empty($this->parent) ? '' : ' AND \'' . $this->parent . '\' in parents';
95
    }
96
}
97