UserDataRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 64
ccs 16
cts 16
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSettingsHash() 0 5 1
A __construct() 0 3 1
A find() 0 9 2
A save() 0 7 1
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\Repositories;
15
16
use Algolia\AlgoliaSearch\SearchIndex;
17
18
/**
19
 * @internal
20
 */
21
final class UserDataRepository
22
{
23
    /**
24
     * @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository
25
     */
26
    private $remoteRepository;
27
28
    /**
29
     * UserDataRepository constructor.
30
     *
31
     * @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository
32
     */
33 10
    public function __construct(RemoteSettingsRepository $remoteRepository)
34
    {
35 10
        $this->remoteRepository = $remoteRepository;
36 10
    }
37
38
    /**
39
     * Find the User Data of the given Index.
40
     *
41
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
42
     *
43
     * @return array
44
     */
45 10
    public function find(SearchIndex $index): array
46
    {
47 10
        $settings = $this->remoteRepository->getSettingsRaw($index);
48
49 10
        if (array_key_exists('userData', $settings)) {
50 10
            $userData = @json_decode($settings['userData'], true);
51
        }
52
53 10
        return $userData ?? [];
54
    }
55
56
    /**
57
     * Save the User Data of the given Index.
58
     *
59
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
60
     * @param  array $userData
61
     *
62
     * @return void
63
     */
64 6
    public function save(SearchIndex $index, array $userData): void
65
    {
66 6
        $currentUserData = $this->find($index);
67
68 6
        $userDataJson = json_encode(array_merge($currentUserData, $userData));
69
70 6
        $index->setSettings(['userData' => $userDataJson])->wait();
71 6
    }
72
73
    /**
74
     * Get the settings hash.
75
     *
76
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
77
     *
78
     * @return string
79
     */
80 10
    public function getSettingsHash(SearchIndex $index): string
81
    {
82 10
        $userData = $this->find($index);
83
84 10
        return $userData['settingsHash'] ?? '';
85
    }
86
}
87