Completed
Pull Request — develop (#333)
by Jens
14:58
created

DefaultCorrelationIdProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Helper;
7
8
use Ramsey\Uuid\Uuid;
9
10
class DefaultCorrelationIdProvider implements CorrelationIdProvider
11
{
12
    private $projectKey;
13
14 39
    public function __construct($projectKey = null)
15
    {
16 39
        $projectKey = !empty($projectKey) ? $projectKey : 'php';
17 39
        $this->projectKey = $projectKey;
18 39
    }
19
20 475
    public function getCorrelationId()
21
    {
22 475
        return sprintf('%s/%s', $this->projectKey, Uuid::uuid4()->toString());
23
    }
24
25
    /**
26
     * Returns an instance if ramsay\uuid package is installed
27
     * @param string $projectKey
28
     * @return DefaultCorrelationIdProvider|null
29
     */
30 39
    public static function of($projectKey)
31
    {
32 39
        if (class_exists('\Ramsey\Uuid\Uuid')) {
33 39
            return new DefaultCorrelationIdProvider($projectKey);
34
        }
35
        return null;
36
    }
37
}
38