Test Failed
Push — develop ( af4094...ace3b9 )
by Nuno
03:30
created

UserDataRepository::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
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\Repositories;
15
16
use Algolia\AlgoliaSearch\Index;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Index was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Algolia\ScoutExtended\Settings\Settings;
18
19
/**
20
 * @internal
21
 */
22
final class UserDataRepository
23
{
24
    /**
25
     * @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository
26
     */
27
    private $remoteRepository;
28
29
    /**
30
     * UserDataRepository constructor.
31
     *
32
     * @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository
33
     */
34
    public function __construct(RemoteSettingsRepository $remoteRepository)
35
    {
36
        $this->remoteRepository = $remoteRepository;
37
    }
38
39
    /**
40
     * Find the User Data of the given Index.
41
     *
42
     * @param  \Algolia\AlgoliaSearch\Index $index
43
     *
44
     * @return array
45
     */
46
    public function find(Index $index): array
47
    {
48
        $settings = $this->remoteRepository->getSettingsRaw($index);
49
50
        if (array_key_exists('userData', $settings)) {
51
            $userData = @json_decode($settings['userData']);
52
        }
53
54
        return $userData ?? [];
55
    }
56
57
    /**
58
     * Save the User Data of the given Index.
59
     *
60
     * @param  \Algolia\AlgoliaSearch\Index $index
61
     * @param  array $userData
62
     *
63
     * @return void
64
     */
65
    public function save(Index $index, array $userData): void
66
    {
67
        $currentUserData = $this->find($index);
68
69
        $userDataJson = json_encode(array_merge($currentUserData, $userData));
70
71
        $index->setSettings(['userData' => $userDataJson])->wait();
72
    }
73
}
74