Completed
Push — master ( ed9cfc...60f6d2 )
by
unknown
12s queued 10s
created

SourcesBehavior   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 33
lcom 0
cbo 3
dl 0
loc 115
ccs 52
cts 65
cp 0.8
rs 9.3999
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSources() 0 12 3
D getSource() 0 74 30
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Behaviors;
4
5
use Craft;
6
use TypeError;
7
use yii\base\Behavior;
8
use craft\base\Model;
9
use NerdsAndCompany\Schematic\Schematic;
10
11
/**
12
 * Schematic Sources Behavior.
13
 *
14
 * Sync Craft Setups.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class SourcesBehavior extends Behavior
23
{
24
    /**
25
     * Get sources based on the indexFrom attribute and return them with the indexTo attribute.
26
     *
27
     * @param string       $fieldType
28
     * @param string|array $sources
29
     * @param string       $indexFrom
30
     * @param string       $indexTo
31
     *
32
     * @return array|string
33
     */
34 7
    public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo)
35
    {
36 7
        $mappedSources = $sources;
37 7
        if (is_array($sources)) {
38 7
            $mappedSources = [];
39 7
            foreach ($sources as $source) {
40 7
                $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
41
            }
42
        }
43
44 7
        return $mappedSources;
45
    }
46
47
    /**
48
     * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
49
     *
50
     * @TODO Break up and simplify this method
51
     *
52
     * @param string $fieldType
53
     * @param string $source
54
     * @param string $indexFrom
55
     * @param string $indexTo
56
     *
57
     * @return string|null
58
     *
59
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
60
     * @SuppressWarnings(PHPMD.NPathComplexity)
61
     */
62 7
    public function getSource(string $fieldType, string $source, string $indexFrom, string $indexTo)
63
    {
64 7
        if (false === strpos($source, ':')) {
65 5
            return $source;
66
        }
67
68
        /** @var Model $sourceObject */
69 7
        $sourceObject = null;
70
71 7
        list($sourceType, $sourceFrom) = explode(':', $source);
72 7
        switch ($sourceType) {
73 7
            case 'section':
74 7
            case 'createEntries':
75 7
            case 'editPeerEntries':
76 7
            case 'deleteEntries':
77 7
            case 'deletePeerEntries':
78 7
            case 'deletePeerEntryDrafts':
79 7
            case 'editEntries':
80 7
            case 'editPeerEntryDrafts':
81 7
            case 'publishEntries':
82 7
            case 'publishPeerEntries':
83 7
            case 'publishPeerEntryDrafts':
84 5
                $service = Craft::$app->sections;
85 5
                $method = 'getSectionBy';
86 5
                break;
87 7
            case 'group':
88 7
            case 'editCategories':
89 3
                $service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
90 3
                $method = 'getGroupBy';
91 3
                break;
92 7
            case 'folder':
93 5
            case 'createFoldersInVolume':
94 5
            case 'deleteFilesAndFoldersInVolume':
95 5
            case 'saveAssetInVolume':
96 5
            case 'viewVolume':
97 5
                $service = Craft::$app->volumes;
98 5
                $method = 'getVolumeBy';
99 5
                break;
100 2
            case 'taggroup':
101
                $service = Craft::$app->tags;
102
                $method = 'getTagGroupBy';
103
                break;
104 2
            case 'field':
105 2
                $service = Craft::$app->fields;
106 2
                $method = 'getFieldBy';
107 2
                break;
108
            case 'editGlobalSet':
109
                $service = Craft::$app->globals;
110
                $method = 'getSetBy';
111
                break;
112
            case 'utility':
113
                return $source;
114
        }
115
116 7
        if (isset($service) && isset($method) && isset($sourceFrom)) {
117 7
            $method = $method.ucfirst($indexFrom);
118
            try {
119 7
                $sourceObject = $service->$method($sourceFrom);
120
            } catch (TypeError $e) {
121
                Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo);
122
                Schematic::error($e->getMessage());
123
124
                return null;
125
            }
126
        }
127
128 7
        if ($sourceObject) {
129 6
            return $sourceType.':'.$sourceObject->$indexTo;
130
        }
131
132 1
        Schematic::warning('No mapping found for source '.$source);
133
134 1
        return $source;
135
    }
136
}
137