Completed
Push — master ( 12bc5d...f69768 )
by Oscar
58:41
created

Minify::forCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr7Middlewares\Middleware;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr7Middlewares\Transformers;
10
use RuntimeException;
11
12 View Code Duplication
class Minify
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
13
{
14
    use Utils\ResolverTrait;
15
16
    /**
17
     * Execute the middleware.
18
     *
19
     * @param ServerRequestInterface $request
20
     * @param ResponseInterface      $response
21
     * @param callable               $next
22
     *
23
     * @return ResponseInterface
24
     */
25
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
26
    {
27
        if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
28
            throw new RuntimeException('Minify middleware needs FormatNegotiator executed before');
29
        }
30
31
        $resolver = $this->resolver ?: new Transformers\Minifier();
32
        $transformer = $resolver->resolve(FormatNegotiator::getFormat($request));
33
34
        if ($transformer) {
35
            $response = $transformer($response);
36
        }
37
38
        return $next($request, $response);
39
    }
40
}
41