ServiceNameExtractor::extract()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ackintosh\Ganesha\GuzzleMiddleware;
3
4
use Psr\Http\Message\RequestInterface;
5
6
class ServiceNameExtractor implements ServiceNameExtractorInterface
7
{
8
    /**
9
     * @var string
10
     */
11
    const OPTION_KEY = 'ganesha.service_name';
12
13
    /**
14
     * @var string
15
     */
16
    const HEADER_NAME = 'X-Ganesha-Service-Name';
17
18
    /**
19
     * @param RequestInterface $request
20
     * @param array $requestOptions
21
     * @return string
22
     */
23
    public function extract(RequestInterface $request, array $requestOptions)
24
    {
25
        if (array_key_exists(self::OPTION_KEY, $requestOptions)) {
26
            return $requestOptions[self::OPTION_KEY];
27
        }
28
29
        $header = $request->getHeader(self::HEADER_NAME);
30
        if (count($header)) {
31
            return $header[0];
32
        }
33
34
        return $request->getUri()->getHost();
35
    }
36
}
37