Passed
Pull Request — master (#7144)
by
unknown
16:21 queued 06:48
created

SearchIndexPathResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexDir() 0 5 1
A __construct() 0 3 1
A ensureIndexDirectoryExists() 0 9 4
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Search\Xapian;
8
9
/**
10
 * Resolves and prepares the filesystem path where the Xapian index is stored.
11
 */
12
final class SearchIndexPathResolver
13
{
14
    public function __construct(
15
        private string $indexDir,
16
    ) {
17
    }
18
19
    /**
20
     * Returns the absolute directory where the Xapian index is stored.
21
     * Ensures that the directory exists on disk.
22
     */
23
    public function getIndexDir(): string
24
    {
25
        $this->ensureIndexDirectoryExists();
26
27
        return $this->indexDir;
28
    }
29
30
    /**
31
     * Ensures that the index directory exists and is writable.
32
     *
33
     * @throws \RuntimeException When the directory cannot be created.
34
     */
35
    public function ensureIndexDirectoryExists(): void
36
    {
37
        if (\is_dir($this->indexDir)) {
38
            return;
39
        }
40
41
        if (!@\mkdir($this->indexDir, 0775, true) && !\is_dir($this->indexDir)) {
42
            throw new \RuntimeException(
43
                \sprintf('Unable to create Xapian index directory: %s', $this->indexDir)
44
            );
45
        }
46
    }
47
}
48