Passed
Push — master ( 855ec7...3048d7 )
by Nuno
04:19
created

ObjectIdEncrypter::getSearchableExploded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
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 count;
17
use function get_class;
18
use Algolia\ScoutExtended\Exceptions\ShouldReimportSearchableException;
19
20
/**
21
 * @internal
22
 */
23
final class ObjectIdEncrypter
24
{
25
    /**
26
     * Holds the metadata separator.
27
     *
28
     * @var string
29
     */
30
    private static $separator = '::';
31
32
    /**
33
     * Encrypt the given searchable.
34
     *
35
     * @param  mixed $searchable
36
     * @param int|null $part
37
     *
38
     * @return string
39
     */
40 16
    public static function encrypt($searchable, int $part = null): string
41
    {
42 16
        $scoutKey = method_exists($searchable, 'getScoutKey') ? $searchable->getScoutKey() : $searchable->getKey();
43
44 16
        $meta = [get_class($searchable->getModel()), $scoutKey];
45
46 16
        if ($part !== null) {
47 5
            $meta[] = $part;
48
        }
49
50 16
        return implode(self::$separator, $meta);
51
    }
52
53
    /**
54
     * @param  string $objectId
55
     *
56
     * @return string
57
     */
58 3
    public static function withoutPart(string $objectId): string
59
    {
60 3
        return implode(self::$separator, [self::decryptSearchable($objectId), self::decryptSearchableKey($objectId)]);
61
    }
62
63
    /**
64
     * @param  string $objectId
65
     *
66
     * @return string
67
     */
68 4
    public static function decryptSearchable(string $objectId): string
69
    {
70 4
        return (string) self::getSearchableExploded($objectId)[0];
71
    }
72
73
    /**
74
     * @param  string $objectId
75
     *
76
     * @return string
77
     */
78 3
    public static function decryptSearchableKey(string $objectId): string
79
    {
80 3
        return (string) self::getSearchableExploded($objectId)[1];
81
    }
82
83
    /**
84
     * @param  string $objectId
85
     *
86
     * @return string[]
87
     */
88 4
    private static function getSearchableExploded(string $objectId): array
89
    {
90 4
        $parts = explode(self::$separator, $objectId);
91
92 4
        if (! is_array($parts) || count($parts) < 2) {
0 ignored issues
show
introduced by
The condition is_array($parts) is always true.
Loading history...
93 1
            throw new ShouldReimportSearchableException('ObjectID seems invalid. You may need to
94
                re-import your data using the `scout-reimport` Artisan command.');
95
        }
96
97 3
        return $parts;
98
    }
99
}
100