Completed
Push — master ( 6c11c5...8b9ad4 )
by Robin
22:29 queued 14:09
created

StreamResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCP\AppFramework\Http;
26
27
use OCP\AppFramework\Http;
28
29
/**
30
 * Class StreamResponse
31
 *
32
 * @package OCP\AppFramework\Http
33
 * @since 8.1.0
34
 */
35
class StreamResponse extends Response implements ICallbackResponse {
36
	/** @var string */
37
	private $filePath;
38
39
	/**
40
	 * @param string|resource $filePath the path to the file or a file handle which should be streamed
41
	 * @since 8.1.0
42
	 */
43
	public function __construct ($filePath) {
44
		$this->filePath = $filePath;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filePath can also be of type resource. However, the property $filePath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45
	}
46
47
48
	/**
49
	 * Streams the file using readfile
50
	 *
51
	 * @param IOutput $output a small wrapper that handles output
52
	 * @since 8.1.0
53
	 */
54
	public function callback (IOutput $output) {
55
		// handle caching
56
		if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
57
			if (!(file_exists($this->filePath) || is_resource($this->filePath))) {
58
				$output->setHttpResponseCode(Http::STATUS_NOT_FOUND);
59
			} elseif ($output->setReadfile($this->filePath) === false) {
60
				$output->setHttpResponseCode(Http::STATUS_BAD_REQUEST);
61
			}
62
		}
63
	}
64
65
}
66