Completed
Push — single_sources ( d082d1...864d3e )
by
unknown
12:38 queued 07:53
created

SourcesBehavior::getSources()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 4
crap 3
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 'single':
75 9
            case 'section':
76 9
            case 'createEntries':
77 9
            case 'editPeerEntries':
78 9
            case 'deleteEntries':
79 9
            case 'deletePeerEntries':
80 9
            case 'deletePeerEntryDrafts':
81 9
            case 'editEntries':
82 9
            case 'editPeerEntryDrafts':
83 9
            case 'publishEntries':
84 9
            case 'publishPeerEntries':
85 9
            case 'publishPeerEntryDrafts':
86 5
                $service = Craft::$app->sections;
87 5
                $method = 'getSectionBy';
88 5
                break;
89 9
            case 'group':
90 7
            case 'editCategories':
91 5
                $service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
92 5
                $method = 'getGroupBy';
93 5
                break;
94 7
            case 'folder':
95 5
            case 'createFoldersInVolume':
96 5
            case 'deleteFilesAndFoldersInVolume':
97 5
            case 'saveAssetInVolume':
98 5
            case 'viewVolume':
99 5
                $service = Craft::$app->volumes;
100 5
                $method = 'getVolumeBy';
101 5
                break;
102 2
            case 'taggroup':
103
                $service = Craft::$app->tags;
104
                $method = 'getTagGroupBy';
105
                break;
106 2
            case 'field':
107 2
                $service = Craft::$app->fields;
108 2
                $method = 'getFieldBy';
109 2
                break;
110
            case 'editGlobalSet':
111
                $service = Craft::$app->globals;
112
                $method = 'getSetBy';
113
                break;
114
            case 'utility':
115
                return $source;
116
        }
117
118 9
        if (isset($service) && isset($method) && isset($sourceFrom)) {
119 9
            $method = $method.ucfirst($indexFrom);
120
            try {
121 9
                $sourceObject = $service->$method($sourceFrom);
122
            } catch (TypeError $e) {
123
                Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo);
124
                Schematic::error($e->getMessage());
125
126
                return null;
127
            }
128
        }
129
130 9
        if ($sourceObject) {
131 8
            return $sourceType.':'.$sourceObject->$indexTo;
132
        }
133
134 1
        Schematic::warning('No mapping found for source '.$source);
135
136 1
        return null;
137
    }
138
}
139