Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

ElementIndexMapper::getMappedSettings()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 13
nc 4
nop 3
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(): array
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
     * {@inheritdoc}
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
     * @TODO: Look into issue with element index settings
55
     * {@inheritdoc}
56
     */
57
    public function import(array $settingDefinitions, array $elementTypes): array
58
    {
59
        Schematic::warning(' - Element index settings may not be imported properly because craft prunes the sources');
60
        foreach ($settingDefinitions as $elementTypeName => $settings) {
61
            $elementType = 'craft\\elements\\'.$elementTypeName;
62
            $mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
63
            if (!Craft::$app->elementIndexes->saveSettings($elementType, $mappedSettings)) {
64
                Schematic::error(' - Settings for '.$elementTypeName.' could not be saved');
65
            }
66
        }
67
68
        return [];
69
    }
70
71
    /**
72
     * Get mapped element index settings, converting source ids to handles or back again.
73
     *
74
     * @param array  $settings
75
     * @param string $fromIndex
76
     * @param string $toIndex
77
     *
78
     * @return array
79
     */
80
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
81
    {
82
        $mappedSettings = ['sourceOrder' => [], 'sources' => []];
83
84
        if (isset($settings['sourceOrder'])) {
85
            foreach ($settings['sourceOrder'] as $row) {
86
                if ('key' == $row[0]) {
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...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...
88
                }
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...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...
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...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...
98
                ];
99
            }
100
        }
101
102
        return $mappedSettings;
103
    }
104
}
105