LocalSettingsRepository::exists()   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\SearchIndex;
17
use Algolia\ScoutExtended\Settings\Settings;
18
use Illuminate\Filesystem\Filesystem;
19
use Illuminate\Support\Str;
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 14
    public function __construct(RemoteSettingsRepository $remoteRepository, Filesystem $files)
43
    {
44 14
        $this->remoteRepository = $remoteRepository;
45 14
        $this->files = $files;
46 14
    }
47
48
    /**
49
     * Checks if the given index settings exists.
50
     *
51
     * @param  \Algolia\AlgoliaSearch\SearchIndex $index
52
     *
53
     * @return bool
54
     */
55 13
    public function exists(SearchIndex $index): bool
56
    {
57 13
        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\SearchIndex $index
64
     *
65
     * @return string
66
     */
67 13
    public function getPath(SearchIndex $index): string
68
    {
69 13
        $name = str_replace('_', '-', $index->getIndexName());
70
71 13
        $name = is_array($name) ? current($name) : $name;
72
73 13
        $fileName = 'scout-'.Str::lower($name).'.php';
74 13
        $settingsPath = config('scout.algolia.settings_path');
75
76 13
        if ($settingsPath === null) {
77 12
            return app('path.config').DIRECTORY_SEPARATOR.$fileName;
0 ignored issues
show
Bug introduced by
Are you sure app('path.config') of type Illuminate\Contracts\Foundation\Application|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            return /** @scrutinizer ignore-type */ app('path.config').DIRECTORY_SEPARATOR.$fileName;
Loading history...
78
        }
79
80 1
        if (! $this->files->exists($settingsPath)) {
81 1
            $this->files->makeDirectory($settingsPath, 0755, true);
82
        }
83
84 1
        return $settingsPath.DIRECTORY_SEPARATOR.$fileName;
85
    }
86
87
    /**
88
     * Find the settings of the given Index.
89
     *
90
     * @param \Algolia\AlgoliaSearch\SearchIndex $index
91
     *
92
     * @return \Algolia\ScoutExtended\Settings\Settings
93
     */
94 8
    public function find(SearchIndex $index): Settings
95
    {
96 8
        return new Settings(($this->exists($index) ? require $this->getPath($index) : []),
97 8
            $this->remoteRepository->defaults());
98
    }
99
}
100