Passed
Push — develop ( 1795ba...79eaf3 )
by Jens
25:35 queued 03:06
created

UserAgentProvider::getAdapterInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
1
<?php
2
3
namespace Commercetools\Core\Client;
4
5
use Commercetools\Core\Client;
6
use GuzzleHttp\Client as HttpClient;
7
8
class UserAgentProvider
9
{
10
    private $userAgent;
11
12
    const USER_AGENT = 'commercetools-sdk-php-v1/';
13
14
    /**
15
     * UserAgentProvider constructor.
16
     * @param string $userAgent
17
     */
18 27
    public function __construct($userAgent = null)
19
    {
20 27
        if (is_null($userAgent)) {
21 27
            $userAgent = self::USER_AGENT . Client::VERSION;
22
23 27
            $userAgent .= ' (' . $this->getAdapterInfo();
24 27
            if (extension_loaded('curl') && function_exists('curl_version')) {
25 27
                $userAgent .= '; curl/' . \curl_version()['version'];
26
            }
27 27
            $userAgent .= ') PHP/' . PHP_VERSION;
28
        }
29 27
        $this->userAgent = $userAgent;
30 27
    }
31
32
    /**
33
     * @return string
34
     */
35 27
    public function getUserAgent()
36
    {
37 27
        return $this->userAgent;
38
    }
39
40 27
    private function getAdapterInfo()
41
    {
42 27
        if (defined('\GuzzleHttp\Client::MAJOR_VERSION')) {
43 27
            $clientVersion = (string) constant(HttpClient::class . '::MAJOR_VERSION');
44
        } else {
45
            $clientVersion = (string) constant(HttpClient::class . '::VERSION');
46
        }
47 27
        return 'GuzzleHttp/' . $clientVersion;
48
    }
49
}
50