Completed
Pull Request — master (#114)
by Bart
02:13
created

ElementIndexMapper::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Mappers;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
7
use NerdsAndCompany\Schematic\Schematic;
8
use NerdsAndCompany\Schematic\Interfaces\MapperInterface;
9
use yii\base\Component as BaseComponent;
10
11
/**
12
 * Schematic Element Index Mapper.
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 ElementIndexMapper extends BaseComponent implements MapperInterface
23
{
24
    /**
25
     * Load sources behaviors.
26
     *
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
          SourcesBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33
        ];
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function export(array $elementTypes): array
40
    {
41
        $settingDefinitions = [];
42
        foreach ($elementTypes as $elementType) {
43
            $settings = Craft::$app->elementIndexes->getSettings($elementType);
44
            if (is_array($settings)) {
45
                $elementTypeName = str_replace('craft\\elements\\', '', $elementType);
46
                $settingDefinitions[$elementTypeName] = $this->getMappedSettings($settings, 'id', 'handle');
47
            }
48
        }
49
50
        return $settingDefinitions;
51
    }
52
53
    /**
54
     * @param array $settingDefinitions
55
     *
56
     * @return Result
57
     */
58
    public function import(array $settingDefinitions, array $elementTypes): array
59
    {
60
        Schematic::warning('Element index import is not yet implemented');
61
62
        foreach ($settingDefinitions as $elementType => $settings) {
63
            $mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
0 ignored issues
show
Unused Code introduced by
$mappedSettings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
            // Import the settings
65
        }
66
67
        return $elementTypes;
68
    }
69
70
    /**
71
     * Get mapped element index settings, converting source ids to handles or back again.
72
     *
73
     * @param array  $settings
74
     * @param string $fromIndex
75
     * @param string $toIndex
76
     *
77
     * @return array
78
     */
79
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
80
    {
81
        $mappedSettings = ['sourceOrder' => [], 'sources' => []];
82
83
        if (isset($settings['sourceOrder'])) {
84
            foreach ($settings['sourceOrder'] as $row) {
85
                if ('key' == $row[0]) {
86
                    $row[1] = $this->getSource(false, $row[1], $fromIndex, $toIndex);
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\S...ers\ElementIndexMapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
87
                }
88
                $mappedSettings['sourceOrder'][] = $row;
89
            }
90
        }
91
92
        if (isset($settings['sources'])) {
93
            foreach ($settings['sources'] as $source => $sourceSettings) {
94
                $mappedSource = $this->getSource(false, $source, $fromIndex, $toIndex);
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\S...ers\ElementIndexMapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
95
                $mappedSettings['sources'][$mappedSource] = [
96
                  'tableAttributes' => $this->getSources('', $sourceSettings['tableAttributes'], $fromIndex, $toIndex),
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\S...ers\ElementIndexMapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
97
                ];
98
            }
99
        }
100
101
        return $mappedSettings;
102
    }
103
}
104