Completed
Pull Request — master (#93)
by Bart
05:17
created

Sources::getSource()   C

Complexity

Conditions 16
Paths 37

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 52
ccs 0
cts 39
cp 0
rs 5.9237
c 1
b 0
f 0
cc 16
eloc 38
nc 37
nop 4
crap 272

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
   public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
32
   {
33
       $mappedSources = $sources;
34
       if (is_array($sources)) {
35
           $mappedSources = [];
36
           foreach ($sources as $source) {
37
               $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
38
           }
39
       }
40
41
       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
   public function getSource($fieldType, $source, $indexFrom, $indexTo)
57
   {
58
       if ($source == 'singles' || $source == '*') {
59
           return $source;
60
       }
61
62
      /** @var BaseElementModel $sourceObject */
63
      $sourceObject = null;
64
65
       if (strpos($source, ':') > -1) {
66
           list($sourceType, $sourceFrom) = explode(':', $source);
67
           switch ($sourceType) {
68
              case 'section':
69
                  $service = Craft::app()->sections;
70
                  $method = 'getSectionBy';
71
                  break;
72
              case 'group':
73
                  $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
74
                  $method = 'getGroupBy';
75
                  break;
76
              case 'folder':
77
                  $service = Craft::app()->schematic_assetSources;
78
                  $method = 'getSourceBy';
79
                  break;
80
              case 'taggroup':
81
                  $service = Craft::app()->tags;
82
                  $method = 'getTagGroupBy';
83
                  break;
84
              case 'field':
85
                  $service = Craft::app()->fields;
86
                  $method = 'getFieldBy';
87
                  break;
88
          }
89
       } elseif ($source !== 'singles') {
90
           //Backwards compatibility
91
          $sourceType = 'section';
92
           $sourceFrom = $source;
93
           $service = Craft::app()->sections;
94
           $method = 'getSectionBy';
95
       }
96
97
       if (isset($service) && isset($method) && isset($sourceFrom)) {
98
           $method = $method.$indexFrom;
99
           $sourceObject = $service->$method($sourceFrom);
100
       }
101
102
       if ($sourceObject && isset($sourceType)) {
103
           return $sourceType.':'.$sourceObject->$indexTo;
104
       }
105
106
       return $source;
107
   }
108
}
109