Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.29% covered (warning)
85.29%
29 / 34
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeGeneratedLog
85.29% covered (warning)
85.29%
29 / 34
77.78% covered (warning)
77.78%
7 / 9
14.62
0.00% covered (danger)
0.00%
0 / 1
 __construct
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOverwriteStrategy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorStacktrace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTargetPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTargetNamespace
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 createRandom
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Apie\Maker\BoundedContext\Resources;
4
5use Apie\Core\ApieLib;
6use Apie\Core\Attributes\Context;
7use Apie\Core\Attributes\FakeCount;
8use Apie\Core\Attributes\FakeMethod;
9use Apie\Core\Attributes\HideIdOnOverview;
10use Apie\Core\Attributes\Not;
11use Apie\Core\Attributes\Requires;
12use Apie\Core\Attributes\RuntimeCheck;
13use Apie\Core\BoundedContext\BoundedContext;
14use Apie\Core\ContextConstants;
15use Apie\Core\Datalayers\ApieDatalayer;
16use Apie\Core\Entities\EntityInterface;
17use Apie\Maker\BoundedContext\Identifiers\CodeGeneratedLogIdentifier;
18use Apie\Maker\BoundedContext\Interfaces\CodeWriterConfigurationInterface;
19use Apie\Maker\BoundedContext\Services\CodeWriter;
20use Apie\Maker\Enums\OverwriteStrategy;
21use Apie\Maker\Utils;
22use DateTimeImmutable;
23use Exception;
24use Faker\Generator;
25use ReflectionClass;
26use Throwable;
27
28#[FakeCount(0)]
29#[HideIdOnOverview]
30#[FakeMethod('createRandom')]
31class CodeGeneratedLog implements EntityInterface, CodeWriterConfigurationInterface
32{
33    private CodeGeneratedLogIdentifier $id;
34
35    private DateTimeImmutable $date;
36
37    private ?string $errorMessage = null;
38
39    private ?string $errorStacktrace = null;
40
41    public function __construct(
42        private OverwriteStrategy $strategy,
43        #[Context()] ApieDatalayer $apieDatalayer,
44        #[Context()] BoundedContext $boundedContext,
45        #[Context()] CodeWriter $codeWriter,
46        #[Context(Utils::MAKER_CONFIG)] private array $makerConfig
47    ) {
48        $this->id = CodeGeneratedLogIdentifier::createRandom();
49        $this->date = ApieLib::getPsrClock()->now();
50        try {
51            $codeWriter->startWriting($this);
52            foreach ($apieDatalayer->all(new ReflectionClass(ResourceDefinition::class), $boundedContext->getId()) as $resourceDefinition) {
53                $boundedContextDefinition = $apieDatalayer->find($resourceDefinition->boundedContextId, $boundedContext->getId());
54                $codeWriter->writeResource($this, $resourceDefinition, $boundedContextDefinition);
55                $codeWriter->writeIdentifier($this, $resourceDefinition, $boundedContextDefinition);
56            }
57        } catch (Throwable $error) {
58            $this->errorMessage = $error->getMessage();
59            $this->errorStacktrace = $error->getTraceAsString();
60        }
61    }
62
63    public function getId(): CodeGeneratedLogIdentifier
64    {
65        return $this->id;
66    }
67
68    public function getDate(): DateTimeImmutable
69    {
70        return $this->date;
71    }
72
73    public function getOverwriteStrategy(): OverwriteStrategy
74    {
75        return $this->strategy;
76    }
77
78    public function getErrorMessage(): ?string
79    {
80        return $this->errorMessage;
81    }
82
83    #[RuntimeCheck(new Not(new Requires(ContextConstants::GET_ALL_OBJECTS)))]
84    public function getErrorStacktrace(): ?string
85    {
86        return $this->errorStacktrace;
87    }
88
89    #[RuntimeCheck(new Not(new Requires(ContextConstants::GET_ALL_OBJECTS)))]
90    public function getTargetPath(): string|false|null
91    {
92        return $this->makerConfig['target_path'] ?? null;
93    }
94
95    #[RuntimeCheck(new Not(new Requires(ContextConstants::GET_ALL_OBJECTS)))]
96    public function getTargetNamespace(string $sub = ''): ?string
97    {
98        if (!isset($this->makerConfig['target_namespace'])) {
99            return null;
100        }
101        if ($sub === '') {
102            return rtrim($this->makerConfig['target_namespace'], '\\');
103        }
104        return rtrim($this->makerConfig['target_namespace'], '\\') . '\\' . ltrim($sub, '\\');
105    }
106
107    public static function createRandom(Generator $faker): self
108    {
109        $res = (new ReflectionClass(__CLASS__))->newInstanceWithoutConstructor();
110        $res->id = $faker->fakeClass(CodeGeneratedLogIdentifier::class);
111        $res->date = $faker->fakeClass(DateTimeImmutable::class);
112        $res->strategy = $faker->fakeClass(OverwriteStrategy::class);
113        if ($faker->boolean()) {
114            $exception = new Exception($faker->text(30));
115            //$exception = $faker->fakeClass(Exception::class);
116            $res->errorMessage = $exception->getMessage();
117            $res->errorStacktrace = $exception->getTraceAsString();
118        } else {
119            $res->errorMessage = $res->errorStacktrace = null;
120        }
121        $res->makerConfig = [
122            'dummy' => $faker->languageCode()
123        ];
124        return $res;
125    }
126}