JsonAssetExporter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 23
ccs 7
cts 7
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A export() 0 9 2
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