Completed
Push — master ( b59ac8...49b0e8 )
by Hannes
09:45 queued 05:16
created

Profiler::getParameters()   C

Complexity

Conditions 14
Paths 25

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 15.292

Importance

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

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 1
    public function __construct(TimeDataCollector $timeline)
37
    {
38 1
        $this->timeline = $timeline;
39 1
    }
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 1
    public function add($start, $end, RequestInterface $request, ResponseInterface $response = null)
48
    {
49 1
        $description = $this->describe($request, $response);
50 1
        $params = $this->getParameters($request, $response);
51
52 1
        $this->timeline->addMeasure($description, $start, $end, $params, 'guzzle');
53 1
    }
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 1
    protected function getParameters(RequestInterface $request, ResponseInterface $response = null)
64
    {
65 1
        $params = [];
66 1
        $result = '';
67
68 1
        $keys = array_intersect($this->context, $this->availableParameters);
69
70 1
        foreach ($keys as $key) {
71
            switch ($key) {
72 1
                case 'method':
73 1
                    $result = $request->getMethod();
74 1
                    break;
75 1
                case 'url':
76 1
                    $result = $request->getUri()->__toString();
77 1
                    break;
78 1
                case 'request_version':
79
                    $result = $request->getProtocolVersion();
80
                    break;
81 1
                case 'response_version':
82
                    $result = $response ? $response->getProtocolVersion() : 'NULL';
83
                    break;
84 1
                case 'host':
85 1
                    $result = $request->getUri()->getHost();
86 1
                    break;
87 1
                case 'hostname':
88
                    $result = gethostname();
89
                    break;
90 1
                case 'status_code':
91 1
                    $result = $response ? $response->getStatusCode() : 'NULL';
92 1
                    break;
93 1
                case 'phrase':
94 1
                    $result = $response ? $response->getReasonPhrase() : 'NULL';
95 1
                    break;
96
            }
97
98 1
            $params[$key] = (string) $result ?: '';
99 1
        }
100
101 1
        return $params;
102
    }
103
}
104