Completed
Pull Request — master (#219)
by Christopher
09:58 queued 04:18
created

getRelationsHasManyKeyNames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 13/02/20
6
 * Time: 4:22 AM.
7
 */
8
namespace AlgoWeb\PODataLaravel\Models;
9
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
14
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
15
use Illuminate\Database\Eloquent\Relations\Relation;
16
use Mockery\Mock;
17
use POData\Common\InvalidOperationException;
18
19
trait MetadataKeyMethodNamesTrait
20
{
21
    protected function polyglotFkKey(Relation $rel)
22
    {
23
        switch (true) {
24
            case $rel instanceof BelongsTo:
25
                return $rel->{$this->checkMethodNameList($rel, ['getForeignKeyName', 'getForeignKey'])}();
26
            case $rel instanceof BelongsToMany:
27
                return $rel->getForeignPivotKeyName();
28
            case $rel instanceof HasOneOrMany:
29
                return $rel->getForeignKeyName();
30
            case $rel instanceof HasManyThrough:
31
                $segments = explode('.', $rel->getQualifiedFarKeyName());
32
                return end($segments);
33
            default:
34
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
35
                throw new InvalidOperationException($msg);
36
        }
37
    }
38
    protected function polyglotRkKey(Relation $rel)
39
    {
40
        switch (true) {
41
            case $rel instanceof BelongsTo:
42
                return $rel->{$this->checkMethodNameList($rel, ['getOwnerKey', 'getOwnerKeyName'])}();
43
            case $rel instanceof BelongsToMany:
44
                return $rel->getRelatedPivotKeyName();
45
            case $rel instanceof HasOneOrMany:
46
                $segments = explode('.', $rel->{$this->checkMethodNameList($rel, ['getLocalKeyName', 'getQualifiedParentKeyName'])}());
47
                return end($segments);
48
            case $rel instanceof HasManyThrough:
49
                $segments = explode('.', $rel->getQualifiedParentKeyName());
50
                return end($segments);
51
            default:
52
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
53
                throw new InvalidOperationException($msg);
54
        }
55
    }
56
57
    protected function polyglotThroughKey(Relation $rel)
58
    {
59
        if (! $rel instanceof HasManyThrough) {
60
            return null;
61
        }
62
        $segments = explode('.', $rel->{$this->checkMethodNameList($rel, ['getThroughKey', 'getQualifiedFirstKeyName'])}());
63
        return end($segments);
64
    }
65
66
    /**
67
     * @param  Model $model
68
     * @return array
69
     */
70
    protected function getModelClassMethods(Model $model)
71
    {
72
        return array_diff(
73
            get_class_methods($model),
74
            get_class_methods(\Illuminate\Database\Eloquent\Model::class),
75
            //TODO: sandi what will happen if Mock is not installed (I.e. Production?)
76
            get_class_methods(Mock::class),
77
            get_class_methods(MetadataTrait::class)
78
        );
79
    }
80
81
    /**
82
     * @param  Relation                  $foo
83
     * @param  array                     $methodList
84
     * @throws InvalidOperationException
85
     * @return string
86
     */
87
    protected function checkMethodNameList(Relation $foo, array $methodList)
88
    {
89
        foreach ($methodList as $methodName) {
90
            if (method_exists($foo, $methodName)) {
91
                return $methodName;
92
            }
93
        }
94
        $msg = 'Expected at least 1 element in related-key list, got 0 for relation %s';
95
        throw new InvalidOperationException(sprintf($msg, get_class($foo)));
96
    }
97
}
98