ImageResponse::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * @author Thomas Tanghus
4
 * @copyright 2013-2014 Thomas Tanghus ([email protected])
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later.
8
 * See the COPYING-README file.
9
 */
10
11
namespace OCA\Contacts;
12
13
use OCP\AppFramework\Http\Response;
14
15
16
/**
17
 * A renderer for images
18
 */
19
class ImageResponse extends Response {
20
	/**
21
	 * @var OCP\Image
22
	 */
23
	protected $image;
24
25
	/**
26
	 * @param \OCP\Image $image
27
	 */
28
	public function __construct($image = null) {
29
		if(!is_null($image)) {
30
			$this->setImage($image);
31
		}
32
	}
33
34
	/**
35
	 * @param OCP\Image $image
36
	 */
37
	public function setImage(\OCP\Image $image) {
38
		if(!$image->valid()) {
39
			throw new \InvalidArgumentException(__METHOD__. ' The image resource is not valid.');
40
		}
41
		$this->image = $image;
0 ignored issues
show
Documentation Bug introduced by
It seems like $image of type object<OCP\Image> is incompatible with the declared type object<OCA\Contacts\OCP\Image> of property $image.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
		$this->addHeader('Content-Type', $image->mimeType());
43
		return $this;
44
	}
45
46
	/**
47
	 * Return the image data stream
48
	 * @return Image data
49
	 */
50
	public function render() {
51
		if(is_null($this->image)) {
52
			throw new \BadMethodCallException(__METHOD__. ' Image must be set either in constructor or with setImage()');
53
		}
54
		return $this->image->data();
55
	}
56
57
}