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

ElementTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 62
loc 62
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\services\traits;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\spark\records\Record;
14
15
/**
16
 * @package flipbox\spark\services\traits
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.1.0
19
 */
20 View Code Duplication
trait ElementTrait
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
    use ObjectTrait;
24
25
    /*******************************************
26
     * ELEMENT -to- RECORD
27
     *******************************************/
28
29
    /**
30
     * @param ElementInterface $element
31
     * @param Record $record
32
     * @param bool $mirrorScenario
33
     */
34
    public function transferToRecord(ElementInterface $element, Record $record, $mirrorScenario = true)
35
    {
36
37
        /** @var $element Element */
38
39
        if ($mirrorScenario === true) {
40
41
            // Mirror scenarios
42
            $record->setScenario($element->getScenario());
43
44
        }
45
46
        // Transfer attributes
47
        $record->setAttributes($element->toArray());
48
49
    }
50
51
    /**
52
     * @param ElementInterface $element
53
     * @param bool $mirrorScenario
54
     * @return Record
55
     */
56
    public function toRecord(ElementInterface $element, $mirrorScenario = true)
57
    {
58
59
        if ($id = $element->getId()) {
60
61
            $record = $this->findRecordByCondition(
62
                ['id' => $id]
63
            );
64
65
        }
66
67
        if (empty($record)) {
68
69
            // Create new record
70
            $record = $this->createRecord();
71
72
        }
73
74
        // Populate the record attributes
75
        $this->transferToRecord($element, $record, $mirrorScenario);
76
77
        return $record;
78
79
    }
80
81
}
82