Configuration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Bicycle\Tesseract\Bridge;
4
5
use Bicycle\Tesseract\Bridge\Exception\ConfigurationException;
6
7
class Configuration
8
{
9
    /** @var string[] */
10
    private const ALLOWED_OPTIONS = ['library_path', 'binary_path', 'capi_header_path'];
11
12
    /** @var array|string[] */
13
    private array $options;
14
15
    /**
16
     * @param array $options
17
     *
18
     * @throws ConfigurationException
19
     */
20
    public function __construct(array $options)
21
    {
22
        $this->validateOptions($options);
23
        $this->options = $options;
24
    }
25
26
    /**
27
     * @return string|null
28
     */
29
    public function getSharedLibraryPath(): ?string
30
    {
31
        return $this->options['library_path'] ?? null;
32
    }
33
34
    /**
35
     * @return string|null
36
     */
37
    public function getCliBinaryPath(): ?string
38
    {
39
        return $this->options['binary_path'] ?? null;
40
    }
41
42
    /**
43
     * @return string|null
44
     */
45
    public function getCApiHeaderpath(): ?string
46
    {
47
        return
48
            $this->options['capi_header_path'] ??
49
                realpath(
50
                    sprintf(
51
                        '%1$s%2$s..%2$s..%2$s..%2$s..%2$sResources%2$sdefinitions%2$stesseract_capi.h',
52
                        __DIR__,
53
                        DIRECTORY_SEPARATOR
54
                    )
55
                );
56
    }
57
58
    /**
59
     * @param array $options
60
     */
61
    private function validateOptions(array $options): void
62
    {
63
        $problematicOptions = [];
64
        foreach (array_keys($options) as $option) {
65
            if (!\is_string($options[$option]) || !in_array($option, static::ALLOWED_OPTIONS, true)) {
66
                $problematicOptions[] = $option;
67
            }
68
        }
69
        $message = sprintf(
70
            'Problem with options %s, allowed options %s',
71
            implode(', ', $problematicOptions),
72
            implode(', ', static::ALLOWED_OPTIONS)
73
        );
74
        if (count($problematicOptions)) {
75
            throw new ConfigurationException($message);
76
        }
77
    }
78
}
79