Completed
Push — master ( d5bc1c...a22fa1 )
by Oscar
05:09
created

IncludeResponse::__invoke()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 7
Ratio 24.14 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 29
rs 8.439
cc 5
eloc 17
nc 5
nop 3
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to include a php with the response.
11
 */
12
class IncludeResponse
13
{
14
    use Utils\FileTrait;
15
    use Utils\StreamTrait;
16
17
    private $continueOnError = false;
18
19
    /**
20
     * Configure if continue to the next middleware if the response has not found.
21
     * 
22
     * @param bool $continueOnError
23
     * 
24
     * @return self
25
     */
26
    public function continueOnError($continueOnError = true)
27
    {
28
        $this->continueOnError = $continueOnError;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Execute the middleware.
35
     *
36
     * @param ServerRequestInterface $request
37
     * @param ResponseInterface      $response
38
     * @param callable               $next
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
43
    {
44
        $file = $this->getFilename($request, 'php');
45
46 View Code Duplication
        if (!is_file($file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            if ($this->continueOnError) {
48
                return $next($request, $response);
49
            }
50
51
            return $response->withStatus(404);
52
        }
53
54
        if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'php') {
55
            $level = ob_get_level();
56
            ob_start();
57
            self::includeFile($file);
58
            $body = self::createStream();
59
            $body->write(Utils\Helpers::getOutput($level));
60
61
            foreach (headers_list() as $header) {
62
                list($name, $value) = array_map('trim', explode(':', $header, 2));
63
                $response = $response->withHeader($name, $value);
64
            }
65
66
            return $response->withBody($body);
67
        }
68
69
        return $next($request, $response);
70
    }
71
72
    /**
73
     * Includes a php file from a static context.
74
     * 
75
     * @param string $file
76
     * 
77
     * @return string
78
     */
79
    private static function includeFile($file)
80
    {
81
        include $file;
82
    }
83
}
84