Test Failed
Push — develop ( 63ed01...7218b8 )
by Nuno
05:28
created

LocalSettingsRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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 Illuminate\Support\Str;
17
use Algolia\AlgoliaSearch\Index;
18
use Illuminate\Filesystem\Filesystem;
19
use Algolia\ScoutExtended\Settings\Settings;
20
21
/**
22
 * @internal
23
 */
24
final class LocalSettingsRepository
25
{
26
    /**
27
     * @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository
28
     */
29
    private $remoteRepository;
30
31
    /**
32
     * @var \Illuminate\Filesystem\Filesystem
33
     */
34
    private $files;
35
36
    /**
37
     * LocalRepository constructor.
38
     *
39
     * @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository
40
     * @param \Illuminate\Filesystem\Filesystem $files
41
     */
42 5
    public function __construct(RemoteSettingsRepository $remoteRepository, Filesystem $files)
43
    {
44 5
        $this->remoteRepository = $remoteRepository;
45 5
        $this->files = $files;
46 5
    }
47
48
    /**
49
     * Checks if the given index settings exists.
50
     *
51
     * @param  \Algolia\AlgoliaSearch\Index $index
52
     *
53
     * @return bool
54
     */
55 4
    public function exists(Index $index): bool
56
    {
57 4
        return $this->files->exists($this->getPath($index));
58
    }
59
60
    /**
61
     * Get the settings path of the given index name.
62
     *
63
     * @param  \Algolia\AlgoliaSearch\Index $index
64
     *
65
     * @return string
66
     */
67 4
    public function getPath(Index $index): string
68
    {
69 4
        $name = str_replace('_', '-', $index->getIndexName());
70
71 4
        $name = is_array($name) ? current($name) : $name;
72
73 4
        return config_path('scout-'.Str::lower($name).'.php');
74
    }
75
76
    /**
77
     * Find the settings of the given Index.
78
     *
79
     * @param \Algolia\AlgoliaSearch\Index $index
80
     *
81
     * @return \Algolia\ScoutExtended\Settings\Settings
82
     */
83 1
    public function find(Index $index): Settings
84
    {
85 1
        return new Settings(($this->exists($index) ? require $this->getPath($index) : []),
86 1
            $this->remoteRepository->defaults());
87
    }
88
}
89