Passed
Push — master ( 9601bc...40dd50 )
by Evgeniy
02:37
created

JsonAssetExporter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 24
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A export() 0 3 1
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 asset bundles with the values of all properties {@see AssetBundle::jsonSerialize()} to a JSON file.
15
 */
16
final class JsonAssetExporter implements AssetExporterInterface
17
{
18
    /**
19
     * @var string The full path to the target JSON file.
20
     */
21
    private string $targetFile;
22
23
    /**
24
     * @param string $targetFile The full path to the target JSON file.
25
     */
26 5
    public function __construct(string $targetFile)
27
    {
28 5
        $this->targetFile = $targetFile;
29 5
    }
30
31
    /**
32
     * {@inheritDoc}
33
     *
34
     * @throws JsonException If an error occurred during JSON encoding of asset bundles.
35
     * @throws RuntimeException If an error occurred while writing to the JSON file.
36
     */
37 4
    public function export(array $assetBundles): void
38
    {
39 4
        AssetUtil::exportToFile($this->targetFile, Json::encode($assetBundles));
40 3
    }
41
}
42