NegotiateTrait::negotiateHeader()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 3
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Negotiation\AbstractNegotiator;
6
7
/**
8
 * Utilities used by middlewares that use a negotiator.
9
 */
10
trait NegotiateTrait
11
{
12
    /**
13
     * Returns the best value of a header.
14
     *
15
     * @param string             $accept     The header to negotiate
16
     * @param AbstractNegotiator $negotiator
17
     * @param array              $priorities
18
     *
19
     * @return string|null
20
     */
21
    private function negotiateHeader($accept, AbstractNegotiator $negotiator, array $priorities)
22
    {
23
        if (empty($accept) || empty($priorities)) {
24
            return;
25
        }
26
27
        try {
28
            $best = $negotiator->getBest($accept, $priorities);
29
        } catch (\Exception $exception) {
30
            return;
31
        }
32
33
        if ($best) {
34
            return $best->getValue();
35
        }
36
    }
37
}
38