Passed
Push — develop ( 38ffd9...9f7101 )
by Jens
31:57 queued 05:22
created

UserAgentProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 15
dl 0
loc 38
ccs 15
cts 16
cp 0.9375
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserAgent() 0 3 1
A __construct() 0 12 4
A getAdapterInfo() 0 8 2
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
    /**
13
     * UserAgentProvider constructor.
14
     * @param string $userAgent
15
     */
16 27
    public function __construct($userAgent = null)
17
    {
18 27
        if (is_null($userAgent)) {
19 27
            $userAgent = 'commercetools-php-sdk/' . Client::VERSION;
20
21 27
            $userAgent .= ' (' . $this->getAdapterInfo();
22 27
            if (extension_loaded('curl') && function_exists('curl_version')) {
23 27
                $userAgent .= '; curl/' . \curl_version()['version'];
24
            }
25 27
            $userAgent .= ') PHP/' . PHP_VERSION;
26
        }
27 27
        $this->userAgent = $userAgent;
28 27
    }
29
30
    /**
31
     * @return string
32
     */
33 27
    public function getUserAgent()
34
    {
35 27
        return $this->userAgent;
36
    }
37
38 27
    private function getAdapterInfo()
39
    {
40 27
        if (defined('\GuzzleHttp\Client::MAJOR_VERSION')) {
41 27
            $clientVersion = (string) constant(HttpClient::class . '::MAJOR_VERSION');
42
        } else {
43
            $clientVersion = (string) constant(HttpClient::class . '::VERSION');
44
        }
45 27
        return 'GuzzleHttp/' . $clientVersion;
46
    }
47
}
48