Completed
Pull Request — master (#120)
by
unknown
05:55
created

LocalSettingsRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 74
ccs 14
cts 19
cp 0.7368
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
A __construct() 0 4 1
A getPath() 0 18 4
A find() 0 4 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\Repositories;
15
16
use function is_array;
17
use Illuminate\Support\Str;
18
use Illuminate\Filesystem\Filesystem;
19
use Algolia\AlgoliaSearch\SearchIndex;
20
use Algolia\ScoutExtended\Settings\Settings;
21
22
/**
23
 * @internal
24
 */
25
final class LocalSettingsRepository
26
{
27
    /**
28
     * @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository
29
     */
30
    private $remoteRepository;
31
32
    /**
33
     * @var \Illuminate\Filesystem\Filesystem
34
     */
35
    private $files;
36
37
    /**
38
     * LocalRepository constructor.
39
     *
40
     * @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository
41
     * @param \Illuminate\Filesystem\Filesystem $files
42
     */
43 13
    public function __construct(RemoteSettingsRepository $remoteRepository, Filesystem $files)
44
    {
45 13
        $this->remoteRepository = $remoteRepository;
46 13
        $this->files = $files;
47 13
    }
48
49
    /**
50
     * Checks if the given index settings exists.
51
     *
52
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
53
     *
54
     * @return bool
55
     */
56 12
    public function exists(SearchIndex $index): bool
57
    {
58 12
        return $this->files->exists($this->getPath($index));
59
    }
60
61
    /**
62
     * Get the settings path of the given index name.
63
     *
64
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
65
     *
66
     * @return string
67
     */
68 12
    public function getPath(SearchIndex $index): string
69
    {
70 12
        $name = str_replace('_', '-', $index->getIndexName());
71
72 12
        $name = is_array($name) ? current($name) : $name;
73
74 12
        if (config('scout.algolia.settings_path')) {
75
            if (! $this->files->exists(config('scout.algolia.settings_path'))) {
76
                $this->files->makeDirectory(config('scout.algolia.settings_path'), 0755, true);
77
            }
78
79
            return implode(DIRECTORY_SEPARATOR, [
80
                config('scout.algolia.settings_path'),
81
                'scout-'.Str::lower($name).'.php',
82
            ]);
83
        }
84
85 12
        return config_path('scout-'.Str::lower($name).'.php');
86
    }
87
88
    /**
89
     * Find the settings of the given Index.
90
     *
91
     * @param \Algolia\AlgoliaSearch\SearchIndex $index
92
     *
93
     * @return \Algolia\ScoutExtended\Settings\Settings
94
     */
95 8
    public function find(SearchIndex $index): Settings
96
    {
97 8
        return new Settings(($this->exists($index) ? require $this->getPath($index) : []),
98 8
            $this->remoteRepository->defaults());
99
    }
100
}
101