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

ElementIndexSettings::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
7
use NerdsAndCompany\Schematic\Schematic;
8
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
9
use yii\base\Component as BaseComponent;
10
11
/**
12
 * Schematic Element Index Settings Service.
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 ElementIndexSettings extends BaseComponent implements MappingInterface
23 1
{
24
    /**
25 1
     * Load sources behaviors.
26
     *
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31 2
        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 2
        ];
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function export(array $elementTypes = []): array
40
    {
41
        $settingDefinitions = [];
42 1
        $elementTypes = Craft::$app->elements->getAllElementTypes();
43
        foreach ($elementTypes as $elementType) {
44 1
            $settings = Craft::$app->elementIndexes->getSettings($elementType);
45
            if (is_array($settings)) {
46 1
                $elementTypeName = str_replace('craft\\elements\\', '', $elementType);
47 1
                $settingDefinitions[$elementTypeName] = $this->getMappedSettings($settings, 'id', 'handle');
48 1
            }
49 1
        }
50 1
51 1
        return $settingDefinitions;
52
    }
53 1
54
    /**
55
     * @param array $settingDefinitions
56
     *
57
     * @return Result
58
     */
59
    public function import(array $settingDefinitions, array $elementTypes = []): array
60
    {
61 1
        Schematic::warning('Element index import is not yet implemented');
62
63 1
        foreach ($settingDefinitions as $elementType => $settings) {
64
            $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...
65 1
            // Import the settings
66
        }
67
68 1
        return $elementTypes;
69
    }
70
71 1
    /**
72
     * Get mapped element index settings, converting source ids to handles or back again.
73
     *
74 1
     * @param array  $settings
75
     * @param string $fromIndex
76
     * @param string $toIndex
77 1
     *
78
     * @return array
79
     */
80 1
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
81
    {
82
        $mappedSettings = ['sourceOrder' => [], 'sources' => []];
83 1
84 1
        if (isset($settings['sourceOrder'])) {
85 1
            foreach ($settings['sourceOrder'] as $row) {
86 1
                if ($row[0] == 'key') {
87
                    $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...s\ElementIndexSettings>? 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...
88 1
                }
89
                $mappedSettings['sourceOrder'][] = $row;
90
            }
91
        }
92
93
        if (isset($settings['sources'])) {
94
            foreach ($settings['sources'] as $source => $sourceSettings) {
95
                $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...s\ElementIndexSettings>? 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...
96
                $mappedSettings['sources'][$mappedSource] = [
97
                  '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...s\ElementIndexSettings>? 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...
98
                ];
99
            }
100 2
        }
101
102 2
        return $mappedSettings;
103 2
    }
104
}
105