Passed
Pull Request — master (#99)
by
unknown
13:18
created

RepresentationUniquenessGuard::generateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace hiqdev\php\billing\product\invoice;
4
5
class RepresentationUniquenessGuard
6
{
7
    private static array $keys = [];
8
9
    public function ensureUnique(RepresentationInterface $representation): void
10
    {
11
        $key = $this->generateKey($representation);
12
13
        if (isset(self::$keys[$key])) {
14
            throw new DuplicateRepresentationException("Duplicate '$key' representation");
15
        }
16
17
        self::$keys[$key] = true;
18
    }
19
20
    private function generateKey(RepresentationInterface $representation): string
21
    {
22
        $reflect = new \ReflectionClass($representation);
23
24
        return $reflect->getShortName() . ':' . $representation->getType()->getName();
25
    }
26
}
27