Completed
Push — feature/EVO-4597-rabbitmq-hand... ( 6e503b...616297 )
by
unknown
52:22 queued 46:35
created

XDynamicKey::prepareFunctionNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438
Metric Value
dl 0
loc 11
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.0438
1
<?php
2
/**
3
 * Handle x-dynamic-key
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator\ResourceGenerator;
7
8
use Graviton\ExceptionBundle\Exception\XDynamicKeyException;
9
10
/**
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class XDynamicKey
0 ignored issues
show
Coding Style introduced by
XDynamicKey does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
16
{
17
    /**
18
     * @param  array  $fields     array of fields
19
     * @param  string $refMethods string containing "path" to the ref field
20
     * @return array
21
     *
22
     * @throws MethodNotFoundException
23
     */
24 4
    public static function resolveRef($fields, $refMethods)
25
    {
26 4
        $records = [];
27 4
        $functions = self::prepareFunctionNames($refMethods);
28
29 4
        foreach ($fields as $record) {
30 3
            $orgRec = $record;
31 3
            foreach ($functions as $function) {
32 3
                if (method_exists($record, $function)) {
33 2
                    $record = $record->$function();
34 2
                } else {
35 1
                    throw new XDynamicKeyException(
36
                        'x-dynamic-key ref-field could not be resolved: '.$function
37 1
                    );
38
                }
39 2
            }
40
41 2
            if ($record !== null) {
42 2
                $records[$record->getId()] = $orgRec;
43 2
            }
44 3
        }
45
46 3
        return $records;
47
    }
48
49
    /**
50
     * prepares getter methods for every given field name
51
     *
52
     * @param  string $refMethods string containing "path" to the ref field
53
     * @return array
54
     */
55 4
    private static function prepareFunctionNames($refMethods)
56
    {
57 4
        $fieldNames = explode('.', $refMethods);
58
59 4
        $getters = [];
60 4
        foreach ($fieldNames as $field) {
61 4
            array_push($getters, 'get'.ucfirst($field));
62 4
        }
63
64 4
        return $getters;
65
    }
66
}
67