Test Failed
Push — develop ( 63ed01...7218b8 )
by Nuno
05:28
created

ObjectIdEncrypter::encrypt()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 12
    public static function encrypt($searchable, int $part = null): string
38
    {
39 12
        $scoutKey = method_exists($searchable, 'getScoutKey') ? $searchable->getScoutKey() : $searchable->getKey();
40
41 12
        $meta = [get_class($searchable->getModel()), $scoutKey];
42
43 12
        if ($part !== null) {
44 4
            $meta[] = $part;
45
        }
46
47 12
        return implode(self::$separator, $meta);
48
    }
49
50
    /**
51
     * @param  string $objectId
52
     *
53
     * @return string
54
     */
55 2
    public static function decryptSearchable(string $objectId): string
56
    {
57 2
        return (string) explode(self::$separator, $objectId)[0];
58
    }
59
60
    /**
61
     * @param  string $objectId
62
     *
63
     * @return string
64
     */
65 2
    public static function decryptSearchableKey(string $objectId): string
66
    {
67 2
        return (string) explode(self::$separator, $objectId)[1];
68
    }
69
}
70