Passed
Push — master ( e046f3...04364f )
by Nate
02:43
created

ElementRecordAccessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transferToRecord() 16 16 2
B toRecord() 24 24 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\behaviors;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\spark\records\Record;
14
15
/**
16
 * @package flipbox\spark\behaviors
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20 View Code Duplication
class ElementRecordAccessor extends ObjectRecordAccessor
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...
21
{
22
23
    /*******************************************
24
     * ELEMENT -to- RECORD
25
     *******************************************/
26
27
    /**
28
     * @param ElementInterface $element
29
     * @param Record $record
30
     * @param bool $mirrorScenario
31
     */
32
    public function transferToRecord(ElementInterface $element, Record $record, $mirrorScenario = true)
33
    {
34
35
        /** @var $element Element */
36
37
        if ($mirrorScenario === true) {
38
39
            // Mirror scenarios
40
            $record->setScenario($element->getScenario());
41
42
        }
43
44
        // Transfer attributes
45
        $record->setAttributes($element->toArray());
46
47
    }
48
49
    /**
50
     * @param ElementInterface $element
51
     * @param bool $mirrorScenario
52
     * @return Record
53
     */
54
    public function toRecord(ElementInterface $element, $mirrorScenario = true)
55
    {
56
57
        if ($id = $element->getId()) {
58
59
            $record = $this->findRecordByCondition(
60
                ['id' => $id]
61
            );
62
63
        }
64
65
        if (empty($record)) {
66
67
            // Create new record
68
            $record = $this->createRecord();
69
70
        }
71
72
        // Populate the record attributes
73
        $this->transferToRecord($element, $record, $mirrorScenario);
74
75
        return $record;
76
77
    }
78
79
}
80