ZendExpressive::setApplication()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Svycka\Codeception\Lib\Connector;
4
5
use Symfony\Component\BrowserKit\Client;
6
use Symfony\Component\BrowserKit\Request;
7
use Symfony\Component\BrowserKit\Response;
8
use Symfony\Component\BrowserKit\Request as BrowserKitRequest;
9
use Zend\Diactoros\ServerRequest;
10
use Zend\Expressive\Application;
11
use Zend\Diactoros\UploadedFile;
12
13
final class ZendExpressive extends Client
14
{
15
    /**
16
     * @var Application
17
     */
18
    private $application;
19
20
    public function setApplication(Application $application) : void
21
    {
22
        $this->application = $application;
23
    }
24
25
    /**
26
     * @param Request $request
27
     *
28
     * @return Response
29
     * @throws \Exception
30
     */
31 20
    public function doRequest($request) : Response
32
    {
33 20
        $inputStream = fopen('php://memory', 'rb+');
34 20
        $content = $request->getContent();
35 20
        if ($content !== null) {
0 ignored issues
show
introduced by
The condition $content !== null is always true.
Loading history...
36 4
            fwrite($inputStream, $content);
0 ignored issues
show
Bug introduced by
It seems like $inputStream can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

36
            fwrite(/** @scrutinizer ignore-type */ $inputStream, $content);
Loading history...
37 4
            rewind($inputStream);
0 ignored issues
show
Bug introduced by
It seems like $inputStream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

37
            rewind(/** @scrutinizer ignore-type */ $inputStream);
Loading history...
38
        }
39
40 20
        $queryParams = [];
41 20
        $postParams = [];
42 20
        $queryString = parse_url($request->getUri(), PHP_URL_QUERY);
43 20
        if ($queryString !== '') {
44 20
            parse_str($queryString, $queryParams);
45
        }
46 20
        if ($request->getMethod() !== 'GET') {
47 14
            $postParams = $request->getParameters();
48
        }
49
50 20
        $psrRequest = new ServerRequest(
51 20
            $request->getServer(),
52 20
            $this->convertFiles($request->getFiles()),
53 20
            $request->getUri(),
54 20
            $request->getMethod(),
55 20
            $inputStream,
0 ignored issues
show
Bug introduced by
It seems like $inputStream can also be of type false; however, parameter $body of Zend\Diactoros\ServerRequest::__construct() does only seem to accept resource|string|Psr\Http\Message\StreamInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

55
            /** @scrutinizer ignore-type */ $inputStream,
Loading history...
56 20
            $this->extractHeaders($request),
57 20
            $request->getCookies(),
58 20
            $queryParams,
59 20
            $postParams
60
        );
61
62 20
        $cwd = getcwd();
63 20
        chdir(codecept_root_dir());
64 20
        $response = $this->application->handle($psrRequest);
65 20
        chdir($cwd);
66
67 20
        return new Response(
68 20
            $response->getBody(),
69 20
            $response->getStatusCode(),
70 20
            $response->getHeaders()
71
        );
72
    }
73
74 20
    private function convertFiles(array $files) : array
75
    {
76 20
        $fileObjects = [];
77 20
        foreach ($files as $fieldName => $file) {
78 4
            if ($file instanceof UploadedFile) {
79
                $fileObjects[$fieldName] = $file;
80 4
            } elseif (!isset($file['tmp_name']) && !isset($file['name'])) {
81 2
                $fileObjects[$fieldName] = $this->convertFiles($file);
82
            } else {
83 4
                $fileObjects[$fieldName] = new UploadedFile(
84 4
                    $file['tmp_name'],
85 4
                    $file['size'],
86 4
                    $file['error'],
87 4
                    $file['name'],
88 4
                    $file['type']
89
                );
90
            }
91
        }
92 20
        return $fileObjects;
93
    }
94
95 20
    private function extractHeaders(BrowserKitRequest $request) : array
96
    {
97 20
        $headers = [];
98 20
        $server = $request->getServer();
99
100 20
        $contentHeaders = ['Content-Length' => true, 'Content-Md5' => true, 'Content-Type' => true];
101 20
        foreach ($server as $header => $val) {
102 20
            $header = html_entity_decode(implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $header))))), ENT_NOQUOTES);
103
104 20
            if (strpos($header, 'Http-') === 0) {
105 20
                $headers[substr($header, 5)] = $val;
106 20
            } elseif (isset($contentHeaders[$header])) {
107 20
                $headers[$header] = $val;
108
            }
109
        }
110
111 20
        return $headers;
112
    }
113
}
114