Completed
Pull Request — master (#114)
by Bart
07:39
created

SourcesBehavior::getSource()   C

Complexity

Conditions 29
Paths 90

Size

Total Lines 65
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 5.7384
c 0
b 0
f 0
cc 29
eloc 53
nc 90
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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