Passed
Push — master ( 668ff3...1f2ae2 )
by Yash
03:26
created

Storage::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Webelightdev\LaravelDbBackup\BackupStorage\GoogleDrive;
4
5
use Google_Service_Drive_DriveFile;
6
7
class Storage
8
{
9
    protected $client;
10
    protected $drive;
11
    protected $refresh_token;
12
    protected $folder_id;
13
14
    public function __construct(Client $google_client)
15
    {
16
        $this->client = $google_client->client();
17
        $this->drive = $google_client->drive($this->client);
18
        $this->refresh_token = config('dbbackup.storage.disk.googledrive.refresh_token');
19
        $this->folder_id = config('dbbackup.storage.disk.googledrive.backup_folder_id');
20
    }
21
22
    protected function refreshToken()
23
    {
24
        $this->client->refreshToken($this->refresh_token);
25
        $access_token = $this->client->getAccessToken();
26
        $this->client->setAccessToken($access_token);
27
    }
28
29
    public function uploadFile($backup_file)
30
    {
31
        $this->refreshToken();
32
        
33
        $file = new Google_Service_Drive_DriveFile();
34
        $file->setName($backup_file);
35
        $file->setParents([$this->folder_id]);
36
37
        $data = file_get_contents($backup_file);
38
39
        return $this->drive->files->create($file, array(
40
            'data' => $data,
41
            'mimeType' => 'application/octet-stream',
42
            'uploadType' => 'multipart'
43
        ));
44
    }
45
}
46