JsonAssetExporter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Assets\Exporter;
6
7
use JsonException;
8
use RuntimeException;
9
use Yiisoft\Assets\AssetExporterInterface;
10
use Yiisoft\Assets\AssetUtil;
11
use Yiisoft\Json\Json;
12
13
/**
14
 * Exports the file paths of asset bundles {@see AssetBundle::$export} to a JSON file.
15
 */
16
final class JsonAssetExporter implements AssetExporterInterface
17
{
18
    /**
19
     * @param string $targetFile The full path to the target JSON file.
20
     */
21 8
    public function __construct(private string $targetFile)
22
    {
23 8
    }
24
25
    /**
26
     * @inheritDoc
27
     *
28
     * @throws RuntimeException If an error occurred while writing to the JSON file.
29
     */
30 7
    public function export(array $assetBundles): void
31
    {
32
        try {
33 7
            $data = Json::encode(AssetUtil::extractFilePathsForExport($assetBundles));
34 1
        } catch (JsonException $e) {
35 1
            throw new RuntimeException('An error occurred during JSON encoding of asset bundles.', 0, $e);
36
        }
37
38 6
        AssetUtil::exportToFile($this->targetFile, $data);
39
    }
40
}
41