Passed
Push — master ( 36ec95...f2dea1 )
by Nate
07:31
created

ModelTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 59
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transferToRecord() 14 14 2
A toRecord() 22 22 3

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\traits;
10
11
use Craft;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\helpers\QueryHelper;
14
use flipbox\spark\helpers\RecordHelper;
15
use flipbox\spark\models\Model;
16
use flipbox\spark\records\Record;
17
use yii\db\ActiveQuery;
18
19
/**
20
 * @package flipbox\spark\services\traits
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.1.0
23
 */
24 View Code Duplication
trait ModelTrait
0 ignored issues
show
Duplication introduced by
This class 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...
25
{
26
27
    use ObjectTrait;
28
29
    /*******************************************
30
     * Model -to- Record
31
     *******************************************/
32
33
    /**
34
     * @param Model $model
35
     * @param Record $record
36
     * @param bool $mirrorScenario
37
     * @return void
38
     */
39
    public function transferToRecord(Model $model, Record $record, bool $mirrorScenario = true)
40
    {
41
42
        if ($mirrorScenario === true) {
43
44
            // Mirror scenarios
45
            $record->setScenario($model->getScenario());
46
47
        }
48
49
        // Transfer attributes
50
        $record->setAttributes($model->toArray());
51
52
    }
53
54
    /**
55
     * @param Model $model
56
     * @param bool $mirrorScenario
57
     * @return Record
58
     */
59
    public function toRecord(Model $model, bool $mirrorScenario = true): Record
60
    {
61
62
        if ($id = $model->id) {
63
            $record = $this->findRecordByCondition(
64
                ['id' => $id]
65
            );
66
        }
67
68
        if (empty($record)) {
69
70
            // Create new record
71
            $record = $this->createRecord();
72
73
        }
74
75
        // Populate the record attributes
76
        $this->transferToRecord($model, $record, $mirrorScenario);
77
78
        return $record;
79
80
    }
81
82
}
83