AbstractDelegateBuilderHydrator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 89
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDelegateBuilder() 0 9 2
A getDelegateHydrator() 0 9 2
A hydrate() 0 5 1
A build() 0 5 1
1
<?php
2
/**
3
 * Incoming
4
 *
5
 * @author    Trevor Suarez (Rican7)
6
 * @copyright (c) Trevor Suarez
7
 * @link      https://github.com/Rican7/incoming
8
 * @license   MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Incoming\Hydrator;
14
15
use Incoming\Hydrator\Exception\InvalidDelegateException;
16
17
/**
18
 * An abstract builder and hydrator that allows for the building and hydrating
19
 * to be delegated to another callable. By default, a named method is attempted
20
 * to be found, but any callable could be returned through overrides.
21
 *
22
 * @see AbstractDelegateBuilder
23
 * @see AbstractDelegateHydrator
24
 */
25
abstract class AbstractDelegateBuilderHydrator implements Builder, Hydrator
26
{
27
28
    /**
29
     * Constants
30
     */
31
32
    /**
33
     * The name of the default delegate build method.
34
     *
35
     * @var string
36
     */
37
    const DEFAULT_DELEGATE_BUILD_METHOD_NAME = 'buildModel';
38
39
    /**
40
     * The name of the default delegate hydrate method.
41
     *
42
     * @var string
43
     */
44
    const DEFAULT_DELEGATE_HYDRATE_METHOD_NAME = 'hydrateModel';
45
46
47
    /**
48
     * Methods
49
     */
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @param mixed $incoming The input data.
55
     * @return mixed The built model.
56
     */
57 9
    public function build($incoming)
58
    {
59 9
        $callable = $this->getDelegateBuilder();
60
61 6
        return $callable($incoming);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @param mixed $incoming The input data.
68
     * @param mixed $model The model to hydrate.
69
     * @return mixed The hydrated model.
70
     */
71 9
    public function hydrate($incoming, $model)
72
    {
73 9
        $callable = $this->getDelegateHydrator();
74
75 6
        return $callable($incoming, $model);
76
    }
77
78
    /**
79
     * Get the delegate building callable.
80
     *
81
     * Override this method if a custom delegate is desired.
82
     *
83
     * @return callable The delegate builder callable.
84
     * @throws InvalidDelegateException If the delegate isn't callable.
85
     */
86 15
    protected function getDelegateBuilder(): callable
87
    {
88 15
        $delegate = [$this, static::DEFAULT_DELEGATE_BUILD_METHOD_NAME];
89
90 15
        if (!is_callable($delegate, false, $callable_name)) {
91 3
            throw InvalidDelegateException::forNonCallable($callable_name);
92
        }
93
94 12
        return $delegate;
95
    }
96
97
    /**
98
     * Get the delegate hydration callable.
99
     *
100
     * Override this method if a custom delegate is desired.
101
     *
102
     * @return callable The delegate hydrator callable.
103
     * @throws InvalidDelegateException If the delegate isn't callable.
104
     */
105 15
    protected function getDelegateHydrator(): callable
106
    {
107 15
        $delegate = [$this, static::DEFAULT_DELEGATE_HYDRATE_METHOD_NAME];
108
109 15
        if (!is_callable($delegate, false, $callable_name)) {
110 3
            throw InvalidDelegateException::forNonCallable($callable_name);
111
        }
112
113 12
        return $delegate;
114
    }
115
116
    /**
117
     * The delegate build method.
118
     *
119
     * This doc-block and commented out abstract method is provided here to show
120
     * what the delegate method signature WOULD be if PHP allowed the proper
121
     * typing support to enable a generic definition in this manner.
122
     *
123
     * See the class description for more info.
124
     *
125
     * @param IncomingDataType $incoming The input data.
126
     * @return ModelType The built model.
127
     */
128
    // abstract protected function buildModel(IncomingDataType $incoming): ModelType;
129
130
    /**
131
     * The delegate hydrate method.
132
     *
133
     * This doc-block and commented out abstract method is provided here to show
134
     * what the delegate method signature WOULD be if PHP allowed the proper
135
     * typing support to enable a generic definition in this manner.
136
     *
137
     * See the class description for more info.
138
     *
139
     * @param IncomingDataType $incoming The input data.
140
     * @param ModelType $model The model to hydrate.
141
     * @return ModelType The hydrated model.
142
     */
143
    // abstract protected function hydrateModel(IncomingDataType $incoming, ModelType $model): ModelType;
144
}
145