Completed
Push — master ( 3fed4b...dafce2 )
by Nate
16:31 queued 05:53
created

Records::save()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\services;
10
11
use Craft;
12
use flipbox\ember\services\traits\records\Accessor;
13
use flipbox\organizations\Organizations as OrganizationPlugin;
14
use flipbox\organizations\records\Organization as OrganizationRecord;
15
use yii\base\Component;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 *
21
 * @method OrganizationRecord create(array $attributes = [])
22
 * @method OrganizationRecord find($identifier)
23
 * @method OrganizationRecord get($identifier)
24
 * @method OrganizationRecord findByCondition($condition = [])
25
 * @method OrganizationRecord getByCondition($condition = [])
26
 * @method OrganizationRecord findByCriteria($criteria = [])
27
 * @method OrganizationRecord getByCriteria($criteria = [])
28
 * @method OrganizationRecord[] findAllByCondition($condition = [])
29
 * @method OrganizationRecord[] getAllByCondition($condition = [])
30
 * @method OrganizationRecord[] findAllByCriteria($criteria = [])
31
 * @method OrganizationRecord[] getAllByCriteria($criteria = [])
32
 */
33
class Records extends Component
34
{
35
    use Accessor;
36
37
    /**
38
     * @inheritdoc
39
     */
40 3
    public function init()
41
    {
42 3
        $settings = OrganizationPlugin::getInstance()->getSettings();
43 3
        $this->cacheDuration = $settings->recordsCacheDuration;
44 3
        $this->cacheDependency = $settings->recordsCacheDependency;
45
46 3
        parent::init();
47 3
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public static function recordClass(): string
53
    {
54
        return OrganizationRecord::class;
55
    }
56
57
    /**
58
     * @param OrganizationRecord $record
59
     * @return bool
60
     * @throws \Exception
61
     * @throws \yii\db\Exception
62
     */
63
    public function save(OrganizationRecord $record): bool
64
    {
65
        $transaction = Craft::$app->getDb()->beginTransaction();
66
        try {
67
            if (!$record->save()) {
68
                $transaction->rollBack();
69
                return false;
70
            }
71
        } catch (\Exception $e) {
72
            $transaction->rollBack();
73
            throw $e;
74
        }
75
76
        $transaction->commit();
77
        return true;
78
    }
79
}
80