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

UserAgentProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A getUserAgent() 0 3 1
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
    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