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

RemoteSettingsRepository::getSettingsRaw()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 1
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 20
rs 9.9
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
use Algolia\AlgoliaSearch\Interfaces\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Interfaces\ClientInterface 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...
19
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Exceptions\NotFoundException 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...
20
21
/**
22
 * @internal
23
 */
24
final class RemoteSettingsRepository
25
{
26
    /**
27
     * Settings that may be know by other names.
28
     *
29
     * @var array
30
     */
31
    private static $aliases = [
32
        'attributesToIndex' => 'searchableAttributes',
33
    ];
34
35
    /**
36
     * @var \Algolia\AlgoliaSearch\Interfaces\ClientInterface
37
     */
38
    private $client;
39
40
    /**
41
     * @var array
42
     */
43
    private $defaults;
44
45
    /**
46
     * RemoteRepository constructor.
47
     *
48
     * @param \Algolia\AlgoliaSearch\Interfaces\ClientInterface $client
49
     *
50
     * @return void
51
     */
52
    public function __construct(ClientInterface $client)
53
    {
54
        $this->client = $client;
55
    }
56
57
    /**
58
     * Get the default settings.
59
     *
60
     * @return array
61
     */
62
    public function defaults(): array
63
    {
64
        if ($this->defaults === null) {
65
            $indexName = 'temp-laravel-scout-extended';
66
            $index = $this->client->initIndex($indexName);
67
            $this->defaults = $this->getSettingsRaw($index);
68
            $this->client->deleteIndex($indexName);
69
        }
70
71
        return $this->defaults;
72
    }
73
74
    /**
75
     * Find the settings of the given Index.
76
     *
77
     * @param  \Algolia\AlgoliaSearch\Index $index
78
     *
79
     * @return \Algolia\ScoutExtended\Settings\Settings
80
     */
81
    public function find(Index $index): Settings
82
    {
83
        return new Settings($this->getSettingsRaw($index), $this->defaults());
84
    }
85
86
    /**
87
     * @param \Algolia\AlgoliaSearch\Index $index
88
     * @param \Algolia\ScoutExtended\Settings\Settings $settings
89
     *
90
     * @return void
91
     */
92
    public function save(Index $index, Settings $settings): void
93
    {
94
        $index->setSettings($settings->compiled())->wait();
95
    }
96
97
    /**
98
     * @param  \Algolia\AlgoliaSearch\Index $index
99
     *
100
     * @return array
101
     */
102
    public function getSettingsRaw(Index $index): array
103
    {
104
        try {
105
            $settings = $index->getSettings();
106
        } catch (NotFoundException $e) {
107
            $index->saveObject(['objectID' => 'temp'])->wait();
108
            $settings = $index->getSettings();
109
110
            $index->clear();
111
        }
112
113
        foreach (self::$aliases as $from => $to) {
114
            if (array_key_exists($from, $settings)) {
115
                $settings[$to] = $settings[$from];
116
                unset($settings[$from]);
117
            }
118
        }
119
120
        return $settings;
121
    }
122
}
123