CallsTransformation::callCustomTransformation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Cerbero\Transformer;
4
5
use BadMethodCallException;
6
use Cerbero\Transformer\Transformations\AbstractTransformation as Transformation;
7
use Throwable;
8
use Illuminate\Support\Str;
9
10
/**
11
 * The calls transformation trait.
12
 *
13
 */
14
trait CallsTransformation
15
{
16
    /**
17
     * Resolve and call the given transformation
18
     *
19
     * @param string $transformation
20
     * @param array $parameters
21
     * @return mixed
22
     */
23 33
    protected function callTransformation(string $transformation, array $parameters)
24
    {
25
        // Call custom transformation in the current transformer if implemented
26 33
        if (is_callable([$this, $transformation])) {
27 24
            return call_user_func_array([$this, $transformation], $parameters);
28
        }
29
30
        // Call custom transformation class if implemented
31 33
        if (class_exists($customTransformation = $this->qualifyTransformation($transformation, true))) {
32 27
            return $this->callCustomTransformation($customTransformation, $parameters);
33
        }
34
35
        // Call function or method if exists
36 30
        if (is_callable($transformation)) {
37 27
            array_unshift($parameters, $this->value);
38 27
            return $this->callCallableTransformation($transformation, $parameters);
39
        }
40
41
        // Call internal transformations if exists
42 27
        if (class_exists($internalTransformation = $this->qualifyTransformation($transformation))) {
43 24
            return $this->callCustomTransformation($internalTransformation, $parameters);
44
        }
45
46 3
        throw new BadMethodCallException("Unable to call the transformation {$transformation}");
47
    }
48
49
    /**
50
     * Retrieve the fully qualified class name of the given transformation
51
     *
52
     * @param string $transformation
53
     * @param bool $isCustom
54
     * @return string
55
     */
56 33
    protected function qualifyTransformation(string $transformation, bool $isCustom = false): string
57
    {
58 33
        $namespace = $isCustom ? $this->getCustomTransformationNamespace() : 'Cerbero\Transformer\Transformations';
59
60 33
        return Str::finish($namespace, '\\') . ucfirst($transformation) . 'Transformation';
61
    }
62
63
    /**
64
     * Retrieve the custom namespace for transformations
65
     *
66
     * @return string
67
     */
68
    protected function getCustomTransformationNamespace(): string
69
    {
70
        return '';
71
    }
72
73
    /**
74
     * Call the given custom transformation
75
     *
76
     * @param string $transformation
77
     * @param array $parameters
78
     * @return mixed
79
     */
80 27
    protected function callCustomTransformation(string $transformation, array $parameters)
81
    {
82 27
        if (is_subclass_of($transformation, Transformation::class)) {
83 24
            return (new $transformation($this->value, $this->item))->apply($parameters);
84
        }
85
86 3
        throw new BadMethodCallException('Custom transformations need to implement ' . Transformation::class);
87
    }
88
89
    /**
90
     * Call the given callable transformation
91
     *
92
     * @param string $transformation
93
     * @param array $parameters
94
     * @return mixed
95
     */
96 27
    protected function callCallableTransformation(string $transformation, array $parameters)
97
    {
98
        // Try to call a function or a method statically
99
        try {
100 27
            return call_user_func_array($transformation, $parameters);
101 3
        } catch (Throwable $e) {
102
            // Try to call a method from a class instance
103
            try {
104 3
                $callable = explode('::', $transformation);
105 3
                return call_user_func_array([new $callable[0], $callable[1] ?? ''], $parameters);
106 3
            } catch (Throwable $e) {
107 3
                throw new BadMethodCallException("Unable to call {$transformation}: " . $e->getMessage());
108
            }
109
        }
110
    }
111
}
112