JsonAssetExporter::export()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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