HttpTrait::handleResponse()   C
last analyzed

Complexity

Conditions 12
Paths 60

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 28
nc 60
nop 3
dl 0
loc 47
ccs 36
cts 36
cp 1
crap 12
rs 5.1384
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Laravoole\Wrapper;
4
5
use Symfony\Component\HttpFoundation\BinaryFileResponse;
6
7
trait HttpTrait
8
{
9
    protected $accept_gzip = false;
10 12
    protected function handleResponse($response, $illuminateResponse, $accept_encoding = '')
11
    {
12
13 12
        $accept_gzip = $this->accept_gzip && stripos($accept_encoding, 'gzip') !== false;
14
15
        // status
16 12
        $response->status($illuminateResponse->getStatusCode());
17
        // headers
18 12
        $response->header('Server', config('laravoole.base_config.server'));
19 12
        foreach ($illuminateResponse->headers->allPreserveCase() as $name => $values) {
20 12
            foreach ($values as $value) {
21 12
                $response->header($name, $value);
22 6
            }
23 6
        }
24
        // cookies
25 12
        foreach ($illuminateResponse->headers->getCookies() as $cookie) {
26 12
            $response->rawcookie(
27 12
                $cookie->getName(),
28 12
                urlencode($cookie->getValue()),
29 12
                $cookie->getExpiresTime(),
30 12
                $cookie->getPath(),
31 12
                $cookie->getDomain(),
32 12
                $cookie->isSecure(),
33 12
                $cookie->isHttpOnly()
34 6
            );
35 6
        }
36
        // content
37 12
        if ($illuminateResponse instanceof BinaryFileResponse) {
38 12
            $content = function () use ($illuminateResponse) {
39 12
                return $illuminateResponse->getFile()->getPathname();
40 12
            };
41 12
            if ($accept_gzip && isset($response->header['Content-Type'])) {
42 12
            	$size = $illuminateResponse->getFile()->getSize();
0 ignored issues
show
Unused Code introduced by
$size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43 6
            }
44 6
        } else {
45 12
            $content = $illuminateResponse->getContent();
46
            // check gzip
47 12
            if ($accept_gzip && isset($response->header['Content-Type'])) {
48 12
                $mime = $response->header['Content-Type'];
49
50 12
                if (strlen($content) > config('laravoole.base_config.gzip_min_length') && is_mime_gzip($mime)) {
51 12
                    $response->gzip(config('laravoole.base_config.gzip'));
52 6
                }
53 6
            }
54
        }
55 12
        return $this->endResponse($response, $content);
0 ignored issues
show
Bug introduced by
It seems like endResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
    }
57
58
}
59