Completed
Pull Request — master (#6)
by Trevor N.
01:14
created

AbstractDelegateBuilder::getDelegate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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 that allows for the building to be delegated to another
19
 * callable. By default, a named method is attempted to be found, but any
20
 * callable could be returned through overrides.
21
 *
22
 * This enables a lot of interesting uses, most notably this allows builders to
23
 * be created that have strongly type-hinted building arguments while still
24
 * perfectly satisfying the `Builder`. Essentially this allows the bypassing of
25
 * the type variance rules enforced by PHP in a way that provides a
26
 * generics-like definition. Ultimately, if/when PHP gets generics this will no
27
 * longer be necessary, as one could simply implement a builder using typed
28
 * arguments like: `Builder<IncomingDataType, ModelType>`
29
 *
30
 * @link http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
31
 * @link http://en.wikipedia.org/wiki/Generic_programming
32
 */
33 View Code Duplication
abstract class AbstractDelegateBuilder implements Builder
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
{
35
36
    /**
37
     * Constants
38
     */
39
40
    /**
41
     * The name of the default delegate method
42
     *
43
     * @var string
44
     */
45
    const DEFAULT_DELEGATE_METHOD_NAME = 'buildModel';
46
47
48
    /**
49
     * Methods
50
     */
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @param mixed $incoming The input data
56
     * @return mixed The built model
57
     */
58 9
    public function build($incoming)
59
    {
60 9
        $callable = $this->getDelegate();
61
62 6
        return $callable($incoming);
63
    }
64
65
    /**
66
     * Get the delegate building callable
67
     *
68
     * Override this method if a custom delegate is desired
69
     *
70
     * @return callable The delegate builder callable
71
     */
72 9
    protected function getDelegate(): callable
73
    {
74 9
        $delegate = [$this, static::DEFAULT_DELEGATE_METHOD_NAME];
75
76 9
        if (!is_callable($delegate, false, $callable_name)) {
77 3
            throw InvalidDelegateException::forNonCallable($callable_name);
78
        }
79
80 6
        return $delegate;
81
    }
82
83
    /**
84
     * The delegate build method
85
     *
86
     * This doc-block and commented out abstract method is provided here to show
87
     * what the delegate method signature WOULD be if PHP allowed the proper
88
     * typing support to enable a generic definition in this manner
89
     *
90
     * See the class description for more info
91
     *
92
     * @param IncomingDataType $incoming The input data
93
     * @return ModelType The built model
94
     */
95
    // abstract protected function buildModel(IncomingDataType $incoming): ModelType;
96
}
97