Completed
Pull Request — master (#93)
by Bart
02:47
created

Sources::getSource()   C

Complexity

Conditions 16
Paths 37

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 52
rs 5.9237
cc 16
eloc 38
nc 37
nop 4

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
7
/**
8
 * Schematic Sources Service.
9
 *
10
 * Sync Craft Setups.
11
 *
12
 * @author    Nerds & Company
13
 * @copyright Copyright (c) 2015-2017, Nerds & Company
14
 * @license   MIT
15
 *
16
 * @link      http://www.nerds.company
17
 */
18
class Sources extends Base
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: export, import
Loading history...
19
{
20
    /**
21
    * Get sources based on the indexFrom attribute and return them with the indexTo attribute.
22
    *
23
    * @param string       $fieldType
24
    * @param string|array $sources
25
    * @param string       $indexFrom
26
    * @param string       $indexTo
27
    *
28
    * @return array|string
29
    */
30
   public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
31
   {
32
       $mappedSources = $sources;
33
       if (is_array($sources)) {
34
           $mappedSources = [];
35
           foreach ($sources as $source) {
36
               $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
37
           }
38
       }
39
40
       return $mappedSources;
41
   }
42
43
   /**
44
    * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
45
    *
46
    * @TODO Break up and simplify this method
47
    *
48
    * @param string $fieldType
49
    * @param string $source
50
    * @param string $indexFrom
51
    * @param string $indexTo
52
    *
53
    * @return string
54
    */
55
   public function getSource($fieldType, $source, $indexFrom, $indexTo)
56
   {
57
       if ($source == 'singles' || $source == '*') {
58
           return $source;
59
       }
60
61
      /** @var BaseElementModel $sourceObject */
62
      $sourceObject = null;
63
64
       if (strpos($source, ':') > -1) {
65
           list($sourceType, $sourceFrom) = explode(':', $source);
66
           switch ($sourceType) {
67
              case 'section':
68
                  $service = Craft::app()->sections;
69
                  $method = 'getSectionBy';
70
                  break;
71
              case 'group':
72
                  $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
73
                  $method = 'getGroupBy';
74
                  break;
75
              case 'folder':
76
                  $service = Craft::app()->schematic_assetSources;
77
                  $method = 'getSourceBy';
78
                  break;
79
              case 'taggroup':
80
                  $service = Craft::app()->tags;
81
                  $method = 'getTagGroupBy';
82
                  break;
83
              case 'field':
84
                  $service = Craft::app()->fields;
85
                  $method = 'getFieldBy';
86
                  break;
87
          }
88
       } elseif ($source !== 'singles') {
89
           //Backwards compatibility
90
          $sourceType = 'section';
91
           $sourceFrom = $source;
92
           $service = Craft::app()->sections;
93
           $method = 'getSectionBy';
94
       }
95
96
       if (isset($service) && isset($method) && isset($sourceFrom)) {
97
           $method = $method.$indexFrom;
98
           $sourceObject = $service->$method($sourceFrom);
99
       }
100
101
       if ($sourceObject && isset($sourceType)) {
102
           return $sourceType.':'.$sourceObject->$indexTo;
103
       }
104
105
       return '';
106
   }
107
}
108