Converter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub;
6
7
use PhpEpub\Converters\ConverterInterface;
8
9
class Converter
10
{
11
    private readonly string $epubDirectory;
12
13
    /**
14
     * Converter constructor.
15
     *
16
     * @param string $epubDirectory The directory containing the extracted EPUB contents.
17
     * @param array<string, ConverterInterface> $adapters A map of format to converter adapters.
18
     */
19
    public function __construct(string $epubDirectory, private array $adapters)
20
    {
21
        if (! is_dir($epubDirectory)) {
22
            throw new Exception("EPUB directory does not exist: {$epubDirectory}");
23
        }
24
25
        $this->epubDirectory = $epubDirectory;
26
    }
27
28
    /**
29
     * Converts the EPUB to a specified format.
30
     *
31
     * @param string $format The format to convert to (e.g., 'pdf', 'mobi').
32
     * @param string $outputPath The path where the converted file should be saved.
33
     *
34
     * @throws Exception If the conversion fails or the format is not supported.
35
     */
36
    public function convert(string $format, string $outputPath): void
37
    {
38
        if (! isset($this->adapters[$format])) {
39
            throw new Exception("Conversion format not supported: {$format}");
40
        }
41
42
        $adapter = $this->adapters[$format];
43
        $adapter->convert($this->epubDirectory, $outputPath);
44
    }
45
}
46