Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

EncodingNegotiator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 16.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 3
dl 14
loc 86
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncoding() 0 4 1
A __construct() 0 6 2
A encodings() 0 6 1
A __invoke() 0 6 1
A getFromHeader() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Negotiation\EncodingNegotiator as Negotiator;
9
10
/**
11
 * Middleware that returns the client preferred encoding.
12
 */
13
class EncodingNegotiator
14
{
15
    const KEY = 'ENCODING';
16
17
    /**
18
     * @var array Available encodings
19
     */
20
    private $encodings = [
21
        'gzip',
22
        'deflate',
23
    ];
24
25
    /**
26
     * Returns the encoding.
27
     *
28
     * @param ServerRequestInterface $request
29
     *
30
     * @return string|null
31
     */
32
    public static function getEncoding(ServerRequestInterface $request)
33
    {
34
        return Middleware::getAttribute($request, self::KEY);
35
    }
36
37
    /**
38
     * Constructor. Defines de available encodings.
39
     *
40
     * @param array $encodings
41
     */
42
    public function __construct(array $encodings = null)
43
    {
44
        if ($encodings !== null) {
45
            $this->encodings($encodings);
46
        }
47
    }
48
49
    /**
50
     * Configure the available encodings.
51
     *
52
     * @param array $encodings
53
     *
54
     * @return self
55
     */
56
    public function encodings(array $encodings)
57
    {
58
        $this->encodings = $encodings;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Execute the middleware.
65
     *
66
     * @param ServerRequestInterface $request
67
     * @param ResponseInterface      $response
68
     * @param callable               $next
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
73
    {
74
        $request = Middleware::setAttribute($request, self::KEY, $this->getFromHeader($request));
75
76
        return $next($request, $response);
77
    }
78
79
    /**
80
     * Returns the encoding format using the Accept-Encoding header.
81
     *
82
     * @return null|string
83
     */
84 View Code Duplication
    private function getFromHeader(ServerRequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method 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...
85
    {
86
        $accept = $request->getHeaderLine('Accept-Encoding');
87
88
        if (empty($accept)) {
89
            return;
90
        }
91
92
        $encoding = (new Negotiator())->getBest($accept, $this->encodings);
93
94
        if ($encoding) {
95
            return $encoding->getValue();
96
        }
97
    }
98
}
99