RemoteSettingsRepository::find()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Exceptions\NotFoundException;
17
use Algolia\AlgoliaSearch\SearchClient;
18
use Algolia\AlgoliaSearch\SearchIndex;
19
use Algolia\ScoutExtended\Settings\Settings;
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\SearchClient
37
     */
38
    private $client;
39
40
    /**
41
     * @var array
42
     */
43
    private $defaults;
44
45
    /**
46
     * RemoteRepository constructor.
47
     *
48
     * @param \Algolia\AlgoliaSearch\SearchClient $client
49
     *
50
     * @return void
51
     */
52 14
    public function __construct(SearchClient $client)
53
    {
54 14
        $this->client = $client;
55 14
    }
56
57
    /**
58
     * Get the default settings.
59
     *
60
     * @return array
61
     */
62 12
    public function defaults(): array
63
    {
64 12
        if ($this->defaults === null) {
65 12
            $indexName = 'temp-laravel-scout-extended';
66 12
            $index = $this->client->initIndex($indexName);
67 12
            $this->defaults = $this->getSettingsRaw($index);
68 12
            $index->delete();
69
        }
70
71 12
        return $this->defaults;
72
    }
73
74
    /**
75
     * Find the settings of the given Index.
76
     *
77
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
78
     *
79
     * @return \Algolia\ScoutExtended\Settings\Settings
80
     */
81 12
    public function find(SearchIndex $index): Settings
82
    {
83 12
        return new Settings($this->getSettingsRaw($index), $this->defaults());
84
    }
85
86
    /**
87
     * @param \Algolia\AlgoliaSearch\SearchIndex $index
88
     * @param \Algolia\ScoutExtended\Settings\Settings $settings
89
     *
90
     * @return void
91
     */
92 3
    public function save(SearchIndex $index, Settings $settings): void
93
    {
94 3
        $index->setSettings($settings->compiled())->wait();
95 3
    }
96
97
    /**
98
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
99
     *
100
     * @return array
101
     */
102 12
    public function getSettingsRaw(SearchIndex $index): array
103
    {
104
        try {
105 12
            $settings = $index->getSettings();
106
        } catch (NotFoundException $e) {
107
            $index->saveObject(['objectID' => 'temp'])->wait();
108
            $settings = $index->getSettings();
109
110
            $index->clearObjects();
111
        }
112
113 12
        foreach (self::$aliases as $from => $to) {
114 12
            if (array_key_exists($from, $settings)) {
115
                $settings[$to] = $settings[$from];
116 12
                unset($settings[$from]);
117
            }
118
        }
119
120 12
        return $settings;
121
    }
122
}
123