Completed
Pull Request — master (#114)
by Bart
02:13
created

SourcesBehavior::getSource()   C

Complexity

Conditions 30
Paths 98

Size

Total Lines 67
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

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