Elements   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 81
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A elementClass() 0 4 1
A identifierCondition() 0 19 4
A resolve() 0 14 4
A afterSave() 0 18 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/meta/license
6
 * @link       https://www.flipboxfactory.com/software/meta/
7
 */
8
9
namespace flipbox\meta\services;
10
11
use flipbox\ember\helpers\ArrayHelper;
12
use flipbox\ember\helpers\SiteHelper;
13
use flipbox\ember\services\traits\elements\MultiSiteAccessor;
14
use flipbox\meta\db\MetaQuery;
15
use flipbox\meta\elements\Meta as MetaElement;
16
use flipbox\meta\Meta as MetaPlugin;
17
use yii\base\Component;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @method MetaElement create($config = [])
24
 * @method MetaElement find($identifier, int $siteId = null)
25
 * @method MetaElement get($identifier, int $siteId = null)
26
 * @method MetaQuery getQuery($criteria = [])
27
 */
28
class Elements extends Component
29
{
30
    use MultiSiteAccessor;
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public static function elementClass(): string
36
    {
37
        return MetaElement::class;
38
    }
39
40
    /**
41
     * @param $identifier
42
     * @param int|null $siteId
43
     * @return array
44
     */
45
    protected function identifierCondition($identifier, int $siteId = null): array
46
    {
47
        $base = [
48
            'siteId' => SiteHelper::ensureSiteId($siteId),
49
            'status' => null
50
        ];
51
52
        if (is_array($identifier)) {
53
            return array_merge($base, $identifier);
54
        }
55
56
        if (!is_numeric($identifier) && is_string($identifier)) {
57
            $base['handle'] = $identifier;
58
        } else {
59
            $base['id'] = $identifier;
60
        }
61
62
        return $base;
63
    }
64
65
    /**
66
     * @param mixed $organization
67
     * @return MetaElement
68
     */
69
    public function resolve($organization): MetaElement
70
    {
71
        if (is_array($organization) &&
72
            null !== ($id = ArrayHelper::getValue($organization, 'id'))
73
        ) {
74
            return $this->get($id);
75
        }
76
77
        if ($object = $this->find($organization)) {
78
            return $object;
79
        }
80
81
        return $this->create($organization);
82
    }
83
84
    /**
85
     * Perform the business logic after an element has been saved.
86
     *
87
     * @param MetaElement $meta
88
     * @param bool $isNew
89
     */
90
    public function afterSave(MetaElement $meta, bool $isNew)
91
    {
92
        $associationService = MetaPlugin::getInstance()->getRecords();
93
94
        if (!$isNew) {
95
            $record = $associationService->get($meta->getId());
96
        } else {
97
            $record = $associationService->create([
98
                'id' => $meta->getId()
99
            ]);
100
        }
101
102
        $record->fieldId = $meta->fieldId;
103
        $record->ownerId = $meta->getOwnerId();
104
        $record->ownerSiteId = $meta->ownerSiteId;
105
        $record->sortOrder = $meta->sortOrder;
106
        $record->associate();
107
    }
108
}
109