Completed
Push — master ( 24a48f...5af3f6 )
by
unknown
10s
created

SourcesBehavior::getSource()   D

Complexity

Conditions 30
Paths 122

Size

Total Lines 74
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 40.129

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 45
cts 58
cp 0.7759
rs 4.8881
c 0
b 0
f 0
cc 30
eloc 59
nc 122
nop 4
crap 40.129

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 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 9
    public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo)
35
    {
36 9
        $mappedSources = $sources;
37 9
        if (is_array($sources)) {
38 7
            $mappedSources = [];
39 7
            $sources = array_filter($sources);
40 7
            foreach ($sources as $source) {
41 7
                $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
42
            }
43
        }
44
45 9
        return $mappedSources;
46
    }
47
48
    /**
49
     * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
50
     *
51
     * @TODO Break up and simplify this method
52
     *
53
     * @param string $fieldType
54
     * @param string $source
55
     * @param string $indexFrom
56
     * @param string $indexTo
57
     *
58
     * @return string|null
59
     *
60
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
61
     * @SuppressWarnings(PHPMD.NPathComplexity)
62
     */
63 9
    public function getSource(string $fieldType, string $source, string $indexFrom, string $indexTo)
64
    {
65 9
        if (false === strpos($source, ':')) {
66 5
            return $source;
67
        }
68
69
        /** @var Model $sourceObject */
70 9
        $sourceObject = null;
71
72 9
        list($sourceType, $sourceFrom) = explode(':', $source);
73 9
        switch ($sourceType) {
74 9
            case 'section':
75 9
            case 'createEntries':
76 9
            case 'editPeerEntries':
77 9
            case 'deleteEntries':
78 9
            case 'deletePeerEntries':
79 9
            case 'deletePeerEntryDrafts':
80 9
            case 'editEntries':
81 9
            case 'editPeerEntryDrafts':
82 9
            case 'publishEntries':
83 9
            case 'publishPeerEntries':
84 9
            case 'publishPeerEntryDrafts':
85 5
                $service = Craft::$app->sections;
86 5
                $method = 'getSectionBy';
87 5
                break;
88 9
            case 'group':
89 7
            case 'editCategories':
90 5
                $service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
91 5
                $method = 'getGroupBy';
92 5
                break;
93 7
            case 'folder':
94 5
            case 'createFoldersInVolume':
95 5
            case 'deleteFilesAndFoldersInVolume':
96 5
            case 'saveAssetInVolume':
97 5
            case 'viewVolume':
98 5
                $service = Craft::$app->volumes;
99 5
                $method = 'getVolumeBy';
100 5
                break;
101 2
            case 'taggroup':
102
                $service = Craft::$app->tags;
103
                $method = 'getTagGroupBy';
104
                break;
105 2
            case 'field':
106 2
                $service = Craft::$app->fields;
107 2
                $method = 'getFieldBy';
108 2
                break;
109
            case 'editGlobalSet':
110
                $service = Craft::$app->globals;
111
                $method = 'getSetBy';
112
                break;
113
            case 'utility':
114
                return $source;
115
        }
116
117 9
        if (isset($service) && isset($method) && isset($sourceFrom)) {
118 9
            $method = $method.ucfirst($indexFrom);
119
            try {
120 9
                $sourceObject = $service->$method($sourceFrom);
121
            } catch (TypeError $e) {
122
                Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo);
123
                Schematic::error($e->getMessage());
124
125
                return null;
126
            }
127
        }
128
129 9
        if ($sourceObject) {
130 8
            return $sourceType.':'.$sourceObject->$indexTo;
131
        }
132
133 1
        Schematic::warning('No mapping found for source '.$source);
134
135 1
        return null;
136
    }
137
}
138