Completed
Push — master ( ad6397...d7b0a5 )
by DAOUDI
02:54 queued 22s
created

ResponseManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sdaoudi
5
 * Date: 25/03/18
6
 * Time: 03:04
7
 */
8
9
namespace SfForward\Manager;
10
11
12
use Psr\Http\Message\ResponseInterface;
13
use SfForward\Util\PublicDirectory;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\HttpFoundation\BinaryFileResponse;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class ResponseManager
19
{
20
	/**
21
	 * @var GuzzleResponse
0 ignored issues
show
Bug introduced by
The type SfForward\Manager\GuzzleResponse 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...
22
	 */
23
	protected $response;
24
25
	/**
26
	 * @var string
27
	 */
28
	protected $projectDir;
29
30
    /**
31
     * ResponseManager constructor.
32
     *
33
     * @param ResponseInterface $response
34
     * @param string $projectDir
35
     */
36
	public function __construct(ResponseInterface $response, $projectDir = __DIR__)
37
	{
38
		$this->response   = $response;
0 ignored issues
show
Documentation Bug introduced by
It seems like $response of type Psr\Http\Message\ResponseInterface is incompatible with the declared type SfForward\Manager\GuzzleResponse of property $response.

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...
39
		$this->projectDir = $projectDir;
40
	}
41
42
	/**
43
	 * @return BinaryFileResponse|Response
44
	 */
45
	public function getResponse()
46
	{
47
		if ($this->response->hasHeader('Content-Disposition')) {
48
			return $this->getBinaryFileResponse();
49
		}
50
51
		return $this->getHttpResponse();
52
	}
53
54
	/**
55
	 * @return BinaryFileResponse
56
	 */
57
	protected function getBinaryFileResponse()
58
	{
59
		$projectDir = PublicDirectory::getPublicDir($this->projectDir);
60
		$filename = $projectDir.mt_rand();
0 ignored issues
show
Bug introduced by
Are you sure $projectDir of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
		$filename = /** @scrutinizer ignore-type */ $projectDir.mt_rand();
Loading history...
61
62
		$fileSystem = new Filesystem();
63
		$fileSystem->dumpFile(
64
			$filename,
65
			$this->response->getBody()->getContents()
66
		);
67
68
		return (
69
			new BinaryFileResponse(
70
				$filename,
71
				$this->response->getStatusCode(),
72
				$this->response->getHeaders()
73
			)
74
		)->deleteFileAfterSend(true);
75
	}
76
77
	/**
78
	 * @return Response
79
	 */
80
	protected function getHttpResponse()
81
	{
82
		$response = new Response($this->response->getBody());
83
		$response->setStatusCode($this->response->getStatusCode());
84
85
		return $response;
86
	}
87
}