Completed
Pull Request — master (#100)
by Bart
04:07
created

Sources::getSource()   C

Complexity

Conditions 30
Paths 98

Size

Total Lines 66
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 30.006

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 66
ccs 52
cts 53
cp 0.9811
rs 5.6666
cc 30
eloc 54
nc 98
nop 4
crap 30.006

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\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 24
   public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
32
   {
33 24
       $mappedSources = $sources;
34 24
       if (is_array($sources)) {
35 24
           $mappedSources = [];
36 24
           foreach ($sources as $source) {
37 22
               $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
38 24
           }
39 24
       }
40
41 24
       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 22
   public function getSource($fieldType, $source, $indexFrom, $indexTo)
57
   {
58 22
       if (strpos($source, ':') === false) {
59 4
           return $source;
60
       }
61
62
       /** @var BaseElementModel $sourceObject */
63 20
       $sourceObject = null;
64
65 20
       list($sourceType, $sourceFrom) = explode(':', $source);
66
       switch ($sourceType) {
67 20
          case 'section':
68 20
          case 'createEntries':
69 20
          case 'deleteEntries':
70 20
          case 'deletePeerEntries':
71 20
          case 'deletePeerEntryDrafts':
72 20
          case 'editEntries':
73 20
          case 'editPeerEntries':
74 20
          case 'editPeerEntryDrafts':
75 20
          case 'publishEntries':
76 20
          case 'publishPeerEntries':
77 20
          case 'publishPeerEntryDrafts':
78 2
              $service = Craft::app()->sections;
79 2
              $method = 'getSectionBy';
80 2
              break;
81 18
          case 'group':
82 18
          case 'editCategories':
83 6
              $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
84 6
              $method = 'getGroupBy';
85 6
              break;
86 12
          case 'folder':
87 12
          case 'createSubfoldersInAssetSource':
88 12
          case 'removeFromAssetSource':
89 12
          case 'uploadToAssetSource':
90 12
          case 'viewAssetSource':
91 4
              $service = Craft::app()->schematic_assetSources;
92 4
              $method = 'getSourceBy';
93 4
              break;
94 8
          case 'taggroup':
95 2
              $service = Craft::app()->tags;
96 2
              $method = 'getTagGroupBy';
97 2
              break;
98 6
          case 'field':
99 2
              $service = Craft::app()->fields;
100 2
              $method = 'getFieldBy';
101 2
              break;
102 4
          case 'editGlobalSet':
103 2
              $service = Craft::app()->globals;
104 2
              $method = 'getSetBy';
105 2
              break;
106 2
          case 'editLocale':
107 2
              return $source;
108
              break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
109
       }
110
111 18
       if (isset($service) && isset($method) && isset($sourceFrom)) {
112 18
           $method = $method.ucfirst($indexFrom);
113 18
           $sourceObject = $service->$method($sourceFrom);
114 18
       }
115
116 18
       if ($sourceObject && isset($sourceType)) {
117 18
           return $sourceType.':'.$sourceObject->$indexTo;
118
       }
119
120
       return '';
121
   }
122
}
123