Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

ModelById   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 36.13 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 4
dl 43
loc 119
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 14 14 3
A findCache() 14 14 3
A findCacheByRecord() 15 15 3
A addToCache() 0 12 2
A findRecordByModel() 0 10 2
A toRecord() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services;
10
11
use flipbox\spark\models\Model as BaseModel;
12
use flipbox\spark\models\ModelWithId;
13
use flipbox\spark\records\Record;
14
use flipbox\spark\records\RecordWithId;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
abstract class ModelById extends Model
21
{
22
23
    use traits\ModelById;
24
25
    /*******************************************
26
     * FIND/GET
27
     *******************************************/
28
29
    /**
30
     * @inheritdoc
31
     */
32 View Code Duplication
    public function find($identifier, string $toScenario = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
35
        if ($model = parent::find($identifier, $toScenario)) {
36
            return $model;
37
        }
38
39
        if (!is_numeric($identifier)) {
40
            return null;
41
        }
42
43
        return $this->findById($identifier, $toScenario);
44
45
    }
46
47
48
    /*******************************************
49
     * CACHE
50
     *******************************************/
51
52
    /**
53
     * @inheritdoc
54
     */
55 View Code Duplication
    public function findCache($identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
58
        if ($model = parent::findCache($identifier)) {
59
            return $model;
60
        }
61
62
        if (!is_numeric($identifier)) {
63
            return null;
64
        }
65
66
        return $this->findCacheById($identifier);
67
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 View Code Duplication
    public function findCacheByRecord(Record $record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
76
        if ($model = parent::findCacheByRecord($record)) {
77
            return $model;
78
        }
79
80
        if (!$record instanceof RecordWithId) {
81
            return null;
82
        }
83
84
        // Check if already in cache by id
85
        return $this->findCacheById($record->id);
86
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function addToCache(BaseModel $model)
93
    {
94
95
        parent::addToCache($model);
96
97
        if ($model instanceof ModelWithId) {
98
            $this->cacheById($model);
99
        }
100
101
        return $this;
102
103
    }
104
105
    /*******************************************
106
     * RECORD
107
     *******************************************/
108
109
    /**
110
     * @param BaseModel $model
111
     * @return Record|null
112
     */
113
    public function findRecordByModel(BaseModel $model)
114
    {
115
116
        if ($model instanceof ModelWithId) {
117
            return $this->findRecordById($model->id);
118
        }
119
120
        return parent::findRecordByModel($model);
121
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function toRecord(BaseModel $model, bool $mirrorScenario = true): Record
128
    {
129
130
        if ($model instanceof ModelWithId) {
131
            return $this->toRecordById($model, $mirrorScenario);
132
        }
133
134
        return parent::toRecord($model, $mirrorScenario);
135
136
    }
137
138
}
139