TemporaryPhoto   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 19
c 1
b 1
f 0
lcom 2
cbo 0
dl 0
loc 174
ccs 0
cts 62
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A processImage() 0 4 1
A isValid() 0 3 2
A getKey() 0 4 1
A getPhoto() 0 4 1
A normalizePhoto() 0 16 4
A getMimeType() 0 3 1
A getImage() 0 3 1
A __construct() 0 7 2
A create() 0 7 2
A cachePhoto() 0 14 3
1
<?php
2
/**
3
 * ownCloud - TemporaryPhoto
4
 *
5
 * @author Thomas Tanghus
6
 * @copyright 2014 Thomas Tanghus ([email protected])
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Contacts\Utils;
24
25
use OCP\ICache;
26
use OCP\Image;
27
28
/**
29
 * This class is used for getting a temporary contact photo for cropping.
30
 *
31
 * Sub-classes must override __construct() and processImage()
32
 */
33
class TemporaryPhoto {
34
35
	const MAX_SIZE = 400;
36
37
	const PHOTO_CURRENT		=	0;
38
	const PHOTO_FILESYSTEM	=	1;
39
	const PHOTO_UPLOADED	=	2;
40
	const PHOTO_USER		=	3;
41
42
	/**
43
	 * @var \OCP\ICache
44
	 */
45
	protected $cache;
46
47
	/**
48
	* @var \OCP\Image
49
	*/
50
	protected $image;
51
52
	/**
53
	* Cache key for temporary storage.
54
	*
55
	* @var string
56
	*/
57
	protected $key;
58
59
60
	/**
61
	* Whether normalizePhoto() has already been called.
62
	*
63
	* @var bool
64
	*/
65
	protected $normalized;
66
67
	/**
68
	* Whether the photo is cached.
69
	*
70
	* @var bool
71
	*/
72
	protected $cached;
73
74
	/**
75
	* Photo loader classes.
76
	*
77
	* @var array
78
	*/
79
    static public $classMap = array(
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
80
		'OCA\\Contacts\\Utils\\TemporaryPhoto\\Contact',
81
		'OCA\\Contacts\\Utils\\TemporaryPhoto\\FileSystem',
82
		'OCA\\Contacts\\Utils\\TemporaryPhoto\\Uploaded',
83
		'OCA\\Contacts\\Utils\\TemporaryPhoto\\User',
84
	);
85
86
	/**
87
	* Always call parents ctor in subclasses:
88
	*   		parent::__construct($cache);
89
	*/
90
	public function __construct(ICache $cache, $key = null) {
91
		$this->cache = $cache;
92
		$this->key = $key;
93
		if (!is_null($key)) {
94
			$this->processImage();
95
		}
96
	}
97
98
	/**
99
	* Returns an instance of a subclass of this class
100
	*
101
	*/
102
	public static function create(ICache $cache, $type = null, $data = null) {
103
		if (isset(self::$classMap[$type])) {
104
			return new self::$classMap[$type]($cache, $data);
105
		} else {
106
			return new self($cache, $data);
107
		}
108
	}
109
110
	/**
111
	* Remove a cached image by key.
112
	*
113
	* @param string $key
114
	*/
115
	public function remove($key) {
116
		return $this->cache->remove($key);
117
	}
118
119
	/**
120
	* Do what's needed to get the image from storage
121
	* depending on the type.
122
	* After this method is called $this->image must hold an
123
	* instance of \OCP\Image.
124
	*/
125
	protected function processImage() {
126
		$this->image = new Image();
127
		$this->image->loadFromData($this->cache->get($this->key));
128
	}
129
130
	/**
131
	* Whether this image is valied
132
	*
133
	* @return bool
134
	*/
135
	public function isValid() {
136
		return (($this->image instanceof Image) && $this->image->valid());
0 ignored issues
show
Bug introduced by
The class OCP\Image does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
137
	}
138
139
	/**
140
	* Get the key to the cache image.
141
	*
142
	* @return string
143
	*/
144
	public function getKey() {
145
		$this->cachePhoto();
146
		return $this->key;
147
	}
148
149
	/**
150
	* Get normalized image.
151
	*
152
	* @return \OCP\Image
153
	*/
154
	public function getPhoto() {
155
		$this->normalizePhoto();
156
		return $this->image;
157
	}
158
159
	/**
160
	* Save image data to cache and return the key
161
	*
162
	* @return string
163
	*/
164
	private function cachePhoto() {
165
		if ($this->cached) {
166
			return;
167
		}
168
169
		if (!$this->image instanceof Image) {
0 ignored issues
show
Bug introduced by
The class OCP\Image does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
170
			$this->processImage();
171
		}
172
		$this->normalizePhoto();
173
174
		$data = $this->image->data();
175
		$this->key = uniqid('photo-');
176
		$this->cache->set($this->key, $data, 600);
177
	}
178
179
	/**
180
	* Resize and rotate the image if needed.
181
	*/
182
	private function normalizePhoto() {
183
		
184
		if($this->normalized) {
185
			return;
186
		}
187
188
		$this->image->fixOrientation();
189
190
		if ($this->image->height() > self::MAX_SIZE
191
			|| $this->image->width() > self::MAX_SIZE
192
		) {
193
			$this->image->resize(self::MAX_SIZE);
194
		}
195
196
		$this->normalized = true;
197
	}
198
199
	public function getMimeType(){
200
		return $this->image->mimeType();
201
	}
202
203
	public function getImage(){
204
		return $this->image;
205
	}
206
}