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

ElementTrait::transferToRecord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 4

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
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