Completed
Pull Request — master (#14)
by Pavel
03:32
created

Sha1Strategy::getEntityPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Cache;
4
5
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
6
use Bankiru\Api\Doctrine\Utility\IdentifierFlattener;
7
8
final class Sha1Strategy implements KeyStrategyInterface
9
{
10
    /** @var  IdentifierFlattener */
11
    private $flattener;
12
    /**
13
     * @var string
14
     */
15
    private $prefix;
16
17
    /**
18
     * Sha1Strategy constructor.
19
     *
20
     * @param IdentifierFlattener $flattener
21
     * @param string              $prefix
22
     */
23
    public function __construct(IdentifierFlattener $flattener, $prefix = '')
24
    {
25
        $this->flattener = $flattener;
26
        $this->prefix    = (string)$prefix;
27
    }
28
29
    /** {@inheritdoc} */
30
    public function getEntityPrefix(ApiMetadata $metadata)
31
    {
32
        return $this->prefix;
33
    }
34
35
    /** {@inheritdoc} */
36
    public function getEntityKey(ApiMetadata $metadata, array $identifier)
37
    {
38
        $flattenIdentifiers = $this->flattener->flattenIdentifier($metadata, $identifier);
39
40
        return sha1(sprintf('%s %s', $metadata->getName(), implode(' ', $flattenIdentifiers)));
41
    }
42
}
43