Dropbox::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nicolas
5
 * Date: 09/02/17
6
 * Time: 23:06
7
 */
8
9
namespace Devgiants\Storage;
10
11
12
use Devgiants\Configuration\ApplicationConfiguration as AppConf;
13
use Devgiants\Model\Storage;
14
use Kunnu\Dropbox\DropboxApp;
0 ignored issues
show
Bug introduced by
The type Kunnu\Dropbox\DropboxApp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Kunnu\Dropbox\Dropbox as DropboxService;
0 ignored issues
show
Bug introduced by
The type Kunnu\Dropbox\Dropbox was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Kunnu\Dropbox\DropboxFile;
0 ignored issues
show
Bug introduced by
The type Kunnu\Dropbox\DropboxFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Kunnu\Dropbox\Models\FolderMetadata;
0 ignored issues
show
Bug introduced by
The type Kunnu\Dropbox\Models\FolderMetadata was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Kunnu\Dropbox\Models\FileMetadata;
0 ignored issues
show
Bug introduced by
The type Kunnu\Dropbox\Models\FileMetadata was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class Dropbox extends Storage {
21
22
	const AUTORENAME = true;
23
24
	/**
25
	 * @var DropboxApp
26
	 */
27
	protected $app;
28
29
	/**
30
	 * @var DropboxService
31
	 */
32
	protected $dropbox;
33
34
	/**
35
	 * @var string
36
	 */
37
	protected $clientId;
38
	/**
39
	 * @var string
40
	 */
41
	protected $clientSecret;
42
43
	/**
44
	 * @var string
45
	 */
46
	protected $accessToken;
47
48
	/**
49
	 * Dropbox constructor.
50
	 *
51
	 * @param array $options
52
	 */
53
	public function __construct( $options ) {
54
		$this->clientId     = $options[ AppConf::CLIENT_ID ];
55
		$this->clientSecret = $options[ AppConf::CLIENT_SECRET ];
56
		$this->accessToken  = $options[ AppConf::ACCESS_TOKEN ];
57
	}
58
59
	public static function getType() {
60
		return 'Dropbox';
61
	}
62
63
	/**
64
	 * @return DropboxApp
65
	 */
66
	public function getApp(): DropboxApp {
67
		return $this->app;
68
	}
69
70
	/**
71
	 * @param DropboxApp $app
72
	 *
73
	 * @return Dropbox
74
	 */
75
	public function setApp( DropboxApp $app ): Dropbox {
76
		$this->app = $app;
77
78
		return $this;
79
	}
80
81
	/**
82
	 * @inheritdoc
83
	 */
84
	public function connect() {
85
		//Configure Dropbox Application
86
		$this->app     = new DropboxApp( $this->clientId, $this->clientSecret, $this->accessToken );
87
		$this->dropbox = new DropboxService( $this->app );
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function put( $localPath, $remotePath, array $params = null ) {
94
95
		$remotePath = $this->buildPath( $remotePath, $params );
96
97
		$dropboxFile = new DropboxFile( $localPath );
98
		$this->dropbox->upload( $dropboxFile, $remotePath, [ 'autorename' => static::AUTORENAME ] );
99
100
		return true;
101
	}
102
103
	/**
104
	 * @inheritdoc
105
	 */
106
	public function getItemsList( $remotePath, array $params = null ) {
107
108
		$remotePath = $this->buildPath( $remotePath, $params );
109
110
		return array_map( function ( $item ) {
111
			if ( ! ( $item instanceof FolderMetadata ) && ! ( $item instanceof FileMetadata ) ) {
112
				throw new \InvalidArgumentException();
113
			}
114
115
			return $item->getName();
116
		}, $this->dropbox->listFolder( $remotePath )->getItems()->all() );
117
118
	}
119
120
	/**
121
	 * @inheritdoc
122
	 */
123
	public function get( $remotePath, $localPath, array $params = null ) {
124
125
		$remotePath = $this->buildPath( $remotePath, $params );
126
127
		$remotePath     = $this->sanitizePath( $remotePath );
128
		$downloadedFile = $this->dropbox->download( $remotePath, $localPath );
0 ignored issues
show
Unused Code introduced by
The assignment to $downloadedFile is dead and can be removed.
Loading history...
129
130
//		//Downloaded File Metadata
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
//		$metadata = $downloadedFile->getMetadata();
132
//		$metadata->getName();
133
134
	}
135
136
	/**
137
	 * Unnecessary here as Dropbox API handle path creation
138
	 */
139
	public function makeDir( $path, $recursive = true ) {
140
	}
141
142
	/**
143
	 * @inheritdoc
144
	 */
145
	public function handleRetention( int $retention, array $params = null ) {
146
		// TODO find best way for configurable architecture
147
		if ( isset( $params[ AppConf::ROOT_DIR ] ) ) {
148
			$metadata = $this->dropbox->listFolder( $params[ AppConf::ROOT_DIR ] );
149
150
			$folders = $metadata->getItems()->all();
151
			if ( ( $foldersNumber = count( $folders ) ) > $retention ) {
152
				$folderNumberToRemove = $foldersNumber - $retention;
153
//
154
				// Prune older directories
155
				for ( $i = 0; $i < $folderNumberToRemove; $i ++ ) {
156
					$this->delete( $folders[ $i ]->getPathLower() );
157
				}
158
			}
159
		}
160
	}
161
162
	/**
163
	 * @inheritdoc
164
	 * // TODO handle recursive
165
	 */
166
	public function delete( $path, $recursive = true ) {
167
		$this->dropbox->delete( $path );
168
	}
169
170
	/**
171
	 * @param $remotePath
172
	 * @param array|null $params
173
	 *
174
	 * @return string
175
	 */
176
	protected function buildPath( $remotePath, array $params = null ) {
177
		// Set path as absolute if param is given
178
		if ( isset( $params[ AppConf::ROOT_DIR ] ) ) {
179
			return $this->sanitizePath( "{$params[AppConf::ROOT_DIR]}/{$remotePath}" );
180
		} else {
181
			return $this->sanitizePath( $remotePath );
182
		}
183
	}
184
185
	/**
186
	 * Unused here
187
	 */
188
	public function close() {
189
	}
190
}