Completed
Push — master ( 0d47c0...ecf131 )
by Jesse
05:02
created

CollectionMappingFailed::shortNameOfThe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property\Relationship;
5
6
use function get_class as classOfThe;
7
use RuntimeException;
8
use function sprintf;
9
use Stratadox\Hydration\Mapping\Property\Relationship\CollectionMappingFailed as The;
10
use Stratadox\HydrationMapping\MapsProperty;
11
use Stratadox\HydrationMapping\UnmappableInput;
12
use function strrchr as endOfThe;
13
use function substr as justThe;
14
use Throwable;
15
16
/**
17
 * Notifies the client code when the collection could not be mapped.
18
 *
19
 * @package Stratadox\Hydrate
20
 * @author Stratadox
21
 */
22
final class CollectionMappingFailed extends RuntimeException implements UnmappableInput
23
{
24
    /**
25
     * Notifies the client code when a collection item could not be hydrated.
26
     *
27
     * @param MapsProperty $mapping   The item mapping that failed.
28
     * @param Throwable    $exception The exception that was encountered.
29
     * @return self                   The collection mapping failure.
30
     */
31
    public static function tryingToMapItem(
32
        MapsProperty $mapping,
33
        Throwable $exception
34
    ): self {
35
        return new self(
36
            sprintf(
37
                'Failed to map the %s items of the `%s` property: %s',
38
                The::shortNameOfThe($mapping),
39
                $mapping->name(),
40
                $exception->getMessage()
41
            ),
42
            0,
43
            $exception
44
        );
45
    }
46
47
    /**
48
     * Notifies the client code when a collection class could not be hydrated.
49
     *
50
     * @param MapsProperty $mapping   The collection mapping that failed.
51
     * @param Throwable    $exception The exception that was encountered.
52
     * @return self                   The collection mapping failure.
53
     */
54
    public static function tryingToMapCollection(
55
        MapsProperty $mapping,
56
        Throwable $exception
57
    ): self {
58
        return new self(
59
            sprintf(
60
                'Failed to map the %s collection of the `%s` property: %s',
61
                The::shortNameOfThe($mapping),
62
                $mapping->name(),
63
                $exception->getMessage()
64
            ),
65
            0,
66
            $exception
67
        );
68
    }
69
70
    /**
71
     * Retrieves the class name without namespace.
72
     *
73
     * @param MapsProperty $mapping The failing mapping instance.
74
     * @return string               The unqualified (short) class name of the mapping instance.
75
     */
76
    private static function shortNameOfThe(MapsProperty $mapping): string
77
    {
78
        return justThe(endOfThe(classOfThe($mapping), '\\'), 1);
79
    }
80
}
81