ElementIndexMapper::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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-2019, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 *
22
 * @method getSources(string $fieldType, $sources, string $indexFrom, string $indexTo)
23
 * @method getSource(string $fieldType, string $source, string $indexFrom, string $indexTo)
24
 */
25
class ElementIndexMapper extends BaseComponent implements MapperInterface
26
{
27
    /**
28
     * Load sources behaviors.
29
     *
30
     * @return array
31
     */
32 2
    public function behaviors(): array
33
    {
34
        return [
35 2
          SourcesBehavior::class,
36
        ];
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function export(array $elementTypes): array
43
    {
44 1
        $settingDefinitions = [];
45 1
        foreach ($elementTypes as $elementType) {
46 1
            $settings = Craft::$app->elementIndexes->getSettings($elementType);
47 1
            if (is_array($settings)) {
48 1
                $settingDefinitions[$elementType] = $this->getMappedSettings($settings, 'id', 'handle');
49
            }
50
        }
51
52 1
        return $settingDefinitions;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function import(array $settingDefinitions, array $elementTypes): array
59
    {
60 1
        foreach ($settingDefinitions as $elementType => $settings) {
61
            // Backwards compatibility
62 1
            if (class_exists('craft\\elements\\'.$elementType)) {
63
                $elementType = 'craft\\elements\\'.$elementType;
64
            }
65 1
            $mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
66 1
            if (!Craft::$app->elementIndexes->saveSettings($elementType, $mappedSettings)) {
67 1
                Schematic::error(' - Settings for '.$elementType.' could not be saved');
68
            }
69
        }
70
71 1
        return [];
72
    }
73
74
    /**
75
     * Get mapped element index settings, converting source ids to handles or back again.
76
     *
77
     * @param array  $settings
78
     * @param string $fromIndex
79
     * @param string $toIndex
80
     *
81
     * @return array
82
     */
83 2
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
84
    {
85 2
        $mappedSettings = ['sourceOrder' => [], 'sources' => []];
86
87 2
        if (isset($settings['sourceOrder'])) {
88 2
            foreach ($settings['sourceOrder'] as $row) {
89 2
                if ('key' == $row[0]) {
90 2
                    $row[1] = $this->getSource('', $row[1], $fromIndex, $toIndex);
91
                }
92 2
                $mappedSettings['sourceOrder'][] = $row;
93
            }
94
        }
95
96 2
        if (isset($settings['sources'])) {
97 2
            foreach ($settings['sources'] as $source => $sourceSettings) {
98 2
                $mappedSource = $this->getSource('', $source, $fromIndex, $toIndex);
99 2
                $mappedSettings['sources'][$mappedSource] = [
100 2
                  'tableAttributes' => $this->getSources('', $sourceSettings['tableAttributes'], $fromIndex, $toIndex),
101
                ];
102
            }
103
        }
104
105 2
        return $mappedSettings;
106
    }
107
}
108