Completed
Push — master ( b65177...660b1e )
by Bart
19s queued 13s
created

Sources::getMappedSources()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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