Test Failed
Pull Request — master (#96)
by
unknown
10:23
created

LaravelCaffeineDripMiddleware::makeRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php namespace GeneaLabs\LaravelCaffeine\Http\Middleware;
2
3
use Closure;
4
use GeneaLabs\LaravelCaffeine\Dripper;
5
use GeneaLabs\LaravelCaffeine\Http\Response;
0 ignored issues
show
Bug introduced by
The type GeneaLabs\LaravelCaffeine\Http\Response 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...
6
use Illuminate\Http\Request;
7
8
class LaravelCaffeineDripMiddleware
9
{
10
    public function handle(Request $request, Closure $next)
11
    {
12
        $response = $next($request);
13
14
        $content = $response->getContent();
15
16
        if (! is_string($content) || strlen(trim($content)) === 0) {
17
            return $response;
18
        }
19
20
        $shouldDripRegexp = $this->makeRegex([
21
            '<meta\s+',
22
            '(name\s*=\s*[\'"]caffeinated[\'"]\s+content\s*=\s*[\'"]false[\'"]',
23
            '|content\s*=\s*[\'"]false[\'"]\s+name\s*=\s*[\'"]caffeinated[\'"])',
24
        ]);
25
26
        $shouldNotDrip = preg_match($shouldDripRegexp, $content);
27
28
        if ($shouldNotDrip) {
29
            return $response;
30
        }
31
32
        $formTokenRegexp = $this->makeRegex([
33
            "<input.*?name\s*=\s*[\'\"]_token[\'\"]",
34
        ]);
35
        $metaTokenRegexp = $this->makeRegex([
36
            "<meta.*?name\s*=\s*[\'\"]csrf[_-]token[\'\"]",
37
        ]);
38
        $hasNoFormToken = ! preg_match($formTokenRegexp, $content);
39
        $hasNoMetaToken = ! preg_match($metaTokenRegexp, $content);
40
41
        if ($hasNoFormToken && $hasNoMetaToken) {
42
            return $response;
43
        }
44
45
        $dripper = (new Dripper);
46
        $content = str_replace(
47
            '</body>',
48
            "{$dripper->html}</body>",
49
            $content
50
        );
51
52
        $this->setContents($response, $content);
53
54
        return $response;
55
    }
56
57
    protected function makeRegex(array $regexp) : string
58
    {
59
        return '/' . implode('', $regexp) . '/';
60
    }
61
62
    /**
63
     * @param $response
64
     * @param $content
65
     */
66
    protected function setContents($response, $content): void
67
    {
68
        $original = $response->original;
69
70
        $response->setContent($content);
71
72
        $response->original = $original;
73
    }
74
}
75