Completed
Push — master ( 49b0e8...1ed855 )
by Hannes
16:46
created

Profiler::getParameters()   C

Complexity

Conditions 14
Paths 25

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 14

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 40
ccs 32
cts 32
cp 1
rs 5.0865
cc 14
eloc 32
nc 25
nop 2
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GuzzleHttp\Profiling\Debugbar;
3
4
use DebugBar\DataCollector\TimeDataCollector;
5
use GuzzleHttp\Profiling\DescriptionMaker;
6
use GuzzleHttp\Profiling\Profiler as ProfilerContract;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Profiler implements ProfilerContract
11
{
12
    use DescriptionMaker;
13
14
    /**
15
     * @var \DebugBar\DataCollector\TimeDataCollector
16
     */
17
    protected $timeline;
18
19
    /**
20
     * @var array
21
     */
22
    protected $availableParameters = [
23
        'method', 'url', 'resource', 'request_version', 'response_version', 'host', 'hostname', 'status_code', 'phrase',
24
    ];
25
26
    /**
27
     * @var array
28
     */
29
    protected $context = ['host', 'method', 'url', 'status_code', 'phrase'];
30
31
    /**
32
     * Public constructor.
33
     *
34
     * @param \DebugBar\DataCollector\TimeDataCollector $timeline
35
     */
36 2
    public function __construct(TimeDataCollector $timeline)
37
    {
38 2
        $this->timeline = $timeline;
39 2
    }
40
41
    /**
42
     * @param float $start
43
     * @param float $end
44
     * @param \Psr\Http\Message\RequestInterface $request
45
     * @param \Psr\Http\Message\ResponseInterface $response
46
     */
47 2
    public function add($start, $end, RequestInterface $request, ResponseInterface $response = null)
48
    {
49 2
        $description = $this->describe($request, $response);
50 2
        $params = $this->getParameters($request, $response);
51
52 2
        $this->timeline->addMeasure($description, $start, $end, $params, 'guzzle');
53 2
    }
54
55
    /**
56
     * Get context fields to add to the time-line entry.
57
     *
58
     * @param \Psr\Http\Message\RequestInterface $request
59
     * @param \Psr\Http\Message\ResponseInterface $response
60
     *
61
     * @return array
62
     */
63 2
    protected function getParameters(RequestInterface $request, ResponseInterface $response = null)
64
    {
65 2
        $params = [];
66 2
        $result = '';
67
68 2
        $keys = array_intersect($this->context, $this->availableParameters);
69
70 2
        foreach ($keys as $key) {
71
            switch ($key) {
72 2
                case 'method':
73 2
                    $result = $request->getMethod();
74 2
                    break;
75 2
                case 'url':
76 2
                    $result = $request->getUri()->__toString();
77 2
                    break;
78 2
                case 'request_version':
79 1
                    $result = $request->getProtocolVersion();
80 1
                    break;
81 2
                case 'response_version':
82 1
                    $result = $response ? $response->getProtocolVersion() : 'NULL';
83 1
                    break;
84 2
                case 'host':
85 2
                    $result = $request->getUri()->getHost();
86 2
                    break;
87 2
                case 'hostname':
88 1
                    $result = gethostname();
89 1
                    break;
90 2
                case 'status_code':
91 2
                    $result = $response ? $response->getStatusCode() : 'NULL';
92 2
                    break;
93 2
                case 'phrase':
94 2
                    $result = $response ? $response->getReasonPhrase() : 'NULL';
95 2
                    break;
96
            }
97
98 2
            $params[$key] = (string) $result ?: '';
99 2
        }
100
101 2
        return $params;
102
    }
103
}
104