Test Failed
Push — develop ( 3663ad...1f9f00 )
by Nuno
04:47
created

ObjectIdEncrypter::encrypt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Searchable;
15
16
use function get_class;
17
18
/**
19
 * @internal
20
 */
21
final class ObjectIdEncrypter
22
{
23
    /**
24
     * Holds the metadata separator.
25
     *
26
     * @var string
27
     */
28
    private static $separator = '::';
29
30
    /**
31
     * Encrypt the given searchable.
32
     *
33
     * @param  mixed $searchable
34
     *
35
     * @return string
36
     */
37
    public static function encrypt($searchable): string
38
    {
39
        $scoutKey = method_exists($searchable, 'getScoutKey') ? $searchable->getScoutKey() : $searchable->getKey();
40
41
        return implode(self::$separator, [
42
            UuidGenerator::getUuid($searchable->getModel()),
43
            $scoutKey,
44
        ]);
45
    }
46
47
    /**
48
     * @param  string $objectId
49
     *
50
     * @return string
51
     */
52
    public static function decryptSearchableUuid(string $objectId): string
53
    {
54
        return (string) explode(self::$separator, $objectId)[0];
55
    }
56
57
    /**
58
     * @param  string $objectId
59
     *
60
     * @return string
61
     */
62
    public static function decryptSearchableKey(string $objectId): string
63
    {
64
        return (string) explode(self::$separator, $objectId)[1];
65
    }
66
}
67