Test Failed
Push — develop ( 9dccb9...240a5e )
by Nuno
03:56
created

RemoteRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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