Completed
Push — develop ( 11971b...45f15f )
by Nate
06:59
created

SaveElementTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 41
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A runInternal() 0 16 3
A performAction() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\actions\elements;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\ember\actions\PopulateTrait;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.0.0
18
 *
19
 * @method ElementInterface populate(ElementInterface $object);
20
 */
21
trait SaveElementTrait
22
{
23
    use PopulateTrait, ManageElementTrait;
24
25
    /**
26
     * @inheritdoc
27
     * @param ElementInterface $element
28
     * @throws \Throwable
29
     * @throws \craft\errors\ElementNotFoundException
30
     * @throws \yii\base\Exception
31
     * @throws \yii\web\UnauthorizedHttpException
32
     */
33
    public function runInternal(ElementInterface $element)
34
    {
35
        // Populate
36
        $this->populate($element);
37
38
        // Check access
39
        if (($access = $this->checkAccess($element)) !== true) {
40
            return $access;
41
        }
42
43
        if (!$this->performAction($element)) {
44
            return $this->handleFailResponse($element);
45
        }
46
47
        return $this->handleSuccessResponse($element);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     * @param ElementInterface $element
53
     * @throws \Throwable
54
     * @throws \craft\errors\ElementNotFoundException
55
     * @throws \yii\base\Exception
56
     */
57
    protected function performAction(ElementInterface $element): bool
58
    {
59
        return Craft::$app->getElements()->saveElement($element);
60
    }
61
}
62