AssociateDomain   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 68
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 37 5
A performAction() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\craft\domains\actions;
10
11
use flipbox\craft\ember\actions\ManageTrait;
12
use flipbox\craft\ember\helpers\SiteHelper;
13
use flipbox\craft\domains\records\Domain;
14
use yii\base\Action;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since  1.0.0
19
 */
20
class AssociateDomain extends Action
21
{
22
    use ManageTrait,
23
        ResolverTrait;
24
25
    /**
26
     * @var int
27
     */
28
    public $statusCodeSuccess = 201;
29
30
    /**
31
     * @param string $field
32
     * @param string $element
33
     * @param string $domain
34
     * @param string $status
35
     * @param int|null $siteId
36
     * @param int|null $sortOrder
37
     * @return Domain
38
     * @throws \yii\web\HttpException
39
     */
40
    public function run(
41
        string $field,
42
        string $element,
43
        string $domain,
44
        string $status,
45
        int $siteId = null,
46
        int $sortOrder = null
47
    ) {
48
        // Resolve
49
        $field = $this->resolveField($field);
50
        $element = $this->resolveElement($element);
51
52
        $siteId = SiteHelper::ensureSiteId($siteId ?: $element->siteId);
53
54
        // Find existing?
55
        if (!empty($domain)) {
56
            $record = Domain::findOne([
57
                'element' => $element,
58
                'field' => $field,
59
                'domain' => $domain,
60
                'siteId' => $siteId,
61
            ]);
62
        }
63
64
        if (empty($record)) {
65
            $record = new Domain();
66
            $record->setField($field)
67
                ->setElement($element)
68
                ->setSiteId(SiteHelper::ensureSiteId($siteId ?: $element->siteId));
69
        }
70
71
        $record->domain = $domain;
72
        $record->status = $status;
73
        $record->sortOrder = $sortOrder;
74
75
        return $this->runInternal($domain);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     * @param Domain $record
81
     * @throws \Exception
82
     */
83
    protected function performAction(Domain $record): bool
84
    {
85
        return $record->save();
86
    }
87
}
88