Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExceptionStrategy
85.71% covered (warning)
85.71%
12 / 14
60.00% covered (warning)
60.00%
3 / 5
6.10
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCreationMetadata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getModificationMetadata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getResultMetadata
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\Core\Metadata\Strategy;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Context\MetadataFieldHashmap;
6use Apie\Core\Metadata\CompositeMetadata;
7use Apie\Core\Metadata\Fields\GetterMethod;
8use Apie\Core\Metadata\StrategyInterface;
9use Apie\Serializer\Exceptions\ValidationException;
10use ReflectionClass;
11use Throwable;
12
13final class ExceptionStrategy implements StrategyInterface
14{
15    private RegularObjectStrategy $regular;
16
17    public static function supports(ReflectionClass $class): bool
18    {
19        return is_a($class->name, Throwable::class, true);
20    }
21
22    /**
23     * @param ReflectionClass<Throwable> $class
24     */
25    public function __construct(private ReflectionClass $class)
26    {
27        $this->regular = new RegularObjectStrategy($class);
28    }
29
30    public function getCreationMetadata(ApieContext $context): CompositeMetadata
31    {
32        return $this->regular->getCreationMetadata($context);
33    }
34
35    public function getModificationMetadata(ApieContext $context): CompositeMetadata
36    {
37        return $this->regular->getModificationMetadata($context);
38    }
39
40    public function getResultMetadata(ApieContext $context): CompositeMetadata
41    {
42        $fields = [
43            'message' => new GetterMethod($this->class->getMethod('getMessage')),
44            'code' => new GetterMethod($this->class->getMethod('getCode')),
45        ];
46        if ($this->class->name === ValidationException::class) {
47            $fields['errors'] = new GetterMethod($this->class->getMethod('getErrors'));
48        }
49        return new CompositeMetadata(
50            new MetadataFieldHashmap($fields),
51            $this->class
52        );
53    }
54}