Lines 100.00% 5 / 5
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SnapshotFileReader 100.00% 5 / 5 100.00% 2 / 2 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 readSnapshot 100.00% 4 / 4 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Snapshot;
6
7use Psr\Log\LoggerInterface;
8use Psr\Log\NullLogger;
9use Stewart\Codegen\Exception\CodegenError;
10use Stewart\Codegen\Exception\CodegenException;
11
12final readonly class SnapshotFileReader
13{
14    public function __construct(private SnapshotCodec $codec) {}
15
16    /** @throws CodegenException */
17    public function readSnapshot(string $path, LoggerInterface $logger = new NullLogger()): Snapshot
18    {
19        $snapshotJson = is_file($path) ? file_get_contents($path) : false;
20
21        if ($snapshotJson === false) {
22            throw CodegenException::createForReason(CodegenError::SnapshotUnreadable, path: $path);
23        }
24
25        return $this->codec->decodeSnapshot($snapshotJson, $path, $logger);
26    }
27}