Completed
Pull Request — master (#114)
by Bart
09:33 queued 07:46
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
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
                $service = $fieldType == 'Users' ? Craft::$app->userGroups : Craft::$app->categories;
85
                $method = 'getGroupBy';
86
                break;
87
            case 'folder':
88
            case 'createFoldersInVolume':
89
            case 'deleteFilesAndFoldersInVolume':
90
            case 'saveAssetInVolume':
91
            case 'viewVolume':
92
                $service = Craft::$app->volumes;
93
                $method = 'getVolumeBy';
94
                break;
95
            case 'taggroup':
96
                $service = Craft::$app->tags;
97
                $method = 'getTagGroupBy';
98
                break;
99
            case 'field':
100
                $service = Craft::$app->fields;
101
                $method = 'getFieldBy';
102
                break;
103
            case 'editGlobalSet':
104
                $service = Craft::$app->globals;
105
                $method = 'getSetBy';
106
                break;
107
            case 'utility':
108
                return $source;
109
        }
110
111
        if (isset($service) && isset($method) && isset($sourceFrom)) {
112
            $method = $method.ucfirst($indexFrom);
113
            $sourceObject = $service->$method($sourceFrom);
114
        }
115
116
        if ($sourceObject && isset($sourceType)) {
117
            return $sourceType.':'.$sourceObject->$indexTo;
118
        }
119
120
        Schematic::warning('No mapping found for source'.$source);
121
        return $source;
122
    }
123
}
124