Completed
Push — feature/service-wrapper-for-sw... ( faf928...5dac9b )
by Samuel
13:45 queued 01:09
created

XDynamicKey::resolveRef()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 2
Metric Value
c 8
b 1
f 2
dl 0
loc 24
rs 8.5126
cc 5
eloc 14
nc 6
nop 2
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
    public static function resolveRef($fields, $refMethods)
25
    {
26
        $records = [];
27
        $functions = self::prepareFunctionNames($refMethods);
28
29
        foreach ($fields as $record) {
30
            $orgRec = $record;
31
            foreach ($functions as $function) {
32
                if (method_exists($record, $function)) {
33
                    $record = $record->$function();
34
                } else {
35
                    throw new XDynamicKeyException(
36
                        'x-dynamic-key ref-field could not be resolved: '.$function
37
                    );
38
                }
39
            }
40
41
            if ($record !== null) {
42
                $records[$record->getId()] = $orgRec;
43
            }
44
        }
45
46
        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
    private static function prepareFunctionNames($refMethods)
56
    {
57
        $fieldNames = explode('.', $refMethods);
58
59
        $getters = [];
60
        foreach ($fieldNames as $field) {
61
            array_push($getters, 'get'.ucfirst($field));
62
        }
63
64
        return $getters;
65
    }
66
}
67