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

RepresentationUniquenessGuard   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 8
c 2
b 1
f 0
dl 0
loc 20
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateKey() 0 5 1
A ensureUnique() 0 9 2
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