Completed
Pull Request — master (#114)
by Bart
04:26
created

SourcesBehavior   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 0
cbo 3
dl 0
loc 115
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
    public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo)
35
    {
36
        $mappedSources = $sources;
37
        if (is_array($sources)) {
38
            $mappedSources = [];
39
            foreach ($sources as $source) {
40
                $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
41
            }
42
        }
43
44
        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
    public function getSource(string $fieldType, string $source, string $indexFrom, string $indexTo)
63
    {
64
        if (false === strpos($source, ':')) {
65
            return $source;
66
        }
67
68
        /** @var Model $sourceObject */
69
        $sourceObject = null;
70
71
        list($sourceType, $sourceFrom) = explode(':', $source);
72
        switch ($sourceType) {
73
            case 'section':
74
            case 'createEntries':
75
            case 'editPeerEntries':
76
            case 'deleteEntries':
77
            case 'deletePeerEntries':
78
            case 'deletePeerEntryDrafts':
79
            case 'editEntries':
80
            case 'editPeerEntryDrafts':
81
            case 'publishEntries':
82
            case 'publishPeerEntries':
83
            case 'publishPeerEntryDrafts':
84
                $service = Craft::$app->sections;
85
                $method = 'getSectionBy';
86
                break;
87
            case 'group':
88
            case 'editCategories':
89
                $service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
90
                $method = 'getGroupBy';
91
                break;
92
            case 'folder':
93
            case 'createFoldersInVolume':
94
            case 'deleteFilesAndFoldersInVolume':
95
            case 'saveAssetInVolume':
96
            case 'viewVolume':
97
                $service = Craft::$app->volumes;
98
                $method = 'getVolumeBy';
99
                break;
100
            case 'taggroup':
101
                $service = Craft::$app->tags;
102
                $method = 'getTagGroupBy';
103
                break;
104
            case 'field':
105
                $service = Craft::$app->fields;
106
                $method = 'getFieldBy';
107
                break;
108
            case 'editGlobalSet':
109
                $service = Craft::$app->globals;
110
                $method = 'getSetBy';
111
                break;
112
            case 'utility':
113
                return $source;
114
        }
115
116
        if (isset($service) && isset($method) && isset($sourceFrom)) {
117
            $method = $method.ucfirst($indexFrom);
118
            try {
119
                $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
        if ($sourceObject) {
129
            return $sourceType.':'.$sourceObject->$indexTo;
130
        }
131
132
        Schematic::warning('No mapping found for source '.$source);
133
134
        return $source;
135
    }
136
}
137