ContentIdTrait::withContentId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb\Guard;
13
14
trait ContentIdTrait
15
{
16
    /**
17
     * Content id
18
     *
19
     * @var string|null
20
     */
21
    private $contentId;
22
23 9
    private function __construct(?string $contentId)
24
    {
25 9
        $this->contentId = $contentId;
26 9
    }
27
28
    public static function withContentId(string $contentId = null): self
29
    {
30
        if (null === $contentId) {
31
            $contentId = bin2hex(random_bytes(4));
32
        }
33
34
        return new self($contentId);
35
    }
36
37 9
    public static function withoutContentId(): self
38
    {
39 9
        return new self(null);
40
    }
41
42
    public function contentId(): ?string
43
    {
44
        return $this->contentId;
45
    }
46
}
47