Lines 100.00% 17 / 17
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Generator 100.00% 17 / 17 100.00% 3 / 3 3 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 writeFiles 100.00% 11 / 11 100.00% 1 / 1 1
 checkAgainstSnapshot 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen;
6
7use Stewart\Codegen\Emitter\SourceTree;
8use Stewart\Codegen\Exception\CodegenException;
9use Stewart\Codegen\Model\GenerationModelFactory;
10use Stewart\Codegen\Output\CheckResult;
11use Stewart\Codegen\Output\GenerationReport;
12use Stewart\Codegen\Output\OutputDirectory;
13use Stewart\Codegen\Output\ShrinkPolicy;
14use Stewart\Codegen\Snapshot\Snapshot;
15
16final readonly class Generator
17{
18    public function __construct(
19        private GenerationModelFactory $models,
20        private SourceTree $sources,
21    ) {}
22
23    /** @throws CodegenException */
24    public function writeFiles(Snapshot $snapshot, GenerationTarget $target, GenerationOptions $options, ShrinkPolicy $shrinkPolicy): GenerationReport
25    {
26        $model = $this->models->buildModelFromSnapshot($snapshot, $options);
27
28        return new GenerationReport(
29            changes: new OutputDirectory($target->directory)->writeChanges($this->sources->renderFiles($model, $target), $shrinkPolicy),
30            entities: $model->countEntities(),
31            domains: \count($model->listEntityDomains()),
32            services: $model->countServices(),
33            ignored: \count($model->ignoredEntityIds),
34            attributes: $model->countAttributes(),
35            warnings: $model->warnings,
36            haVersion: $snapshot->haVersion,
37        );
38    }
39
40    /** @throws CodegenException */
41    public function checkAgainstSnapshot(Snapshot $snapshot, GenerationTarget $target, GenerationOptions $options): CheckResult
42    {
43        $model = $this->models->buildModelFromSnapshot($snapshot, $options);
44
45        return new CheckResult(
46            new OutputDirectory($target->directory)->planChanges($this->sources->renderFiles($model, $target)),
47            $model->warnings,
48        );
49    }
50}