CreatesIdentities::createDomainId()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Nord\Lumen\Core\Traits;
4
5
use Closure;
6
use Nord\Lumen\Core\Domain\DomainId;
7
use Nord\Lumen\Core\Exceptions\FatalError;
8
9
trait CreatesIdentities
10
{
11
    /**
12
     * @param Closure $objectIdExists
13
     *
14
     * @return DomainId
15
     * @throws FatalError
16
     */
17
    private function createDomainId(Closure $objectIdExists)
18
    {
19
        $numTries = 0;
20
21
        do {
22
            $domainId = new DomainId();
23
24
            if ($numTries++ >= 10) {
25
                throw new FatalError('Failed to generate a unique identifier.');
26
            }
27
        } while (call_user_func($objectIdExists, $domainId->getValue()));
28
29
        return $domainId;
30
    }
31
}
32