src/Store/Model/Relation/MorphOne.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 34
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 2
mnd 0
bc 0
fnc 2
bpm 0
cpm 1
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A MorphOne.getValueByParentKey 0 12 1
A MorphOne.getRelationalFields 0 3 1
1
import Relation from "../Relation";
2
import ForeignKey from "../Field/ForeignKey";
3
4
export default class MorphOne extends Relation {
5
6
    constructor(model) {
7
        super(model);
8
    }
9
10
    get originalValue() {
11
        return this.getValueByParentKey('originalPrimaryKey');
12
    }
13
14
    get value() {
15
        return this.getValueByParentKey('primaryKey');
16
    }
17
18
    getRelationalFields(): Array<ForeignKey> {
19
        return [];
20
    }
21
22
    private getValueByParentKey(parentProperty): unknown {
23
        const type = this.$parent.className;
24
        const id = this.$parent[parentProperty];
25
        const idKeyName = `${this.name}_id`;
26
        const idTypeName = `${this.name}_type`;
27
28
        const lookUpKey = {};
29
        lookUpKey[idKeyName] = id;
30
        lookUpKey[idTypeName] = type;
31
32
        return this.model.find(lookUpKey);
33
    }
34
}