Completed
Pull Request — master (#114)
by Bart
10:00
created

ElementIndexSettings::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use yii\base\Component as BaseComponent;
7
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
8
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
9
10
/**
11
 * Schematic Element Index Settings Service.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class ElementIndexSettings extends BaseComponent implements MappingInterface
22
{
23 1
    /**
24
     * Load sources behaviors
25 1
     *
26
     * @return array
27
     */
28
    public function behaviors()
29
    {
30
        return [
31 2
          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...
32
        ];
33 2
    }
34
35
    //==============================================================================================================
36
    //================================================  EXPORT  ====================================================
37
    //==============================================================================================================
38
39
    /**
40
     * @return array
41
     */
42 1
    public function export()
43
    {
44 1
        $settingDefinitions = [];
45
        $elementTypes = Craft::$app->elements->getAllElementTypes();
46 1
        foreach ($elementTypes as $elementType) {
47 1
            $settings = Craft::$app->elementIndexes->getSettings($elementType);
48 1
            if (is_array($settings)) {
49 1
                $elementTypeName = str_replace('craft\\elements\\', '', $elementType);
50 1
                $settingDefinitions[$elementTypeName] = $this->getMappedSettings($settings, 'id', 'handle');
51 1
            }
52
        }
53 1
54
        return $settingDefinitions;
55
    }
56
57
    //==============================================================================================================
58
    //================================================  IMPORT  ====================================================
59
    //==============================================================================================================
60
61 1
    /**
62
     * @param array $settingDefinitions
63 1
     * @param bool  $force
64
     *
65 1
     * @return Result
66
     */
67
    public function import($force = false, array $settingDefinitions = null)
68 1
    {
69
        Craft::info('Importing Element Index Settings', 'schematic');
70
71 1
        foreach ($settingDefinitions as $elementType => $settings) {
0 ignored issues
show
Bug introduced by
The expression $settingDefinitions of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
72
            $mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
73
            if (!$this->getElementIndexesService()->saveSettings($elementType, $mappedSettings)) {
0 ignored issues
show
Documentation Bug introduced by
The method getElementIndexesService 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...
74 1
                $this->addError('Element Index Settings for {elementType} could not be installed', ['elementType' => $elementType]);
0 ignored issues
show
Documentation Bug introduced by
The method addError 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...
75
            }
76
        }
77 1
78
        return $this->getResultModel();
0 ignored issues
show
Documentation Bug introduced by
The method getResultModel 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...
79
    }
80 1
81
    /**
82
     * Get mapped element index settings, converting source ids to handles or back again.
83 1
     *
84 1
     * @param array  $settings
85 1
     * @param string $fromIndex
86 1
     * @param string $toIndex
87
     *
88 1
     * @return array
89
     */
90
    private function getMappedSettings(array $settings, $fromIndex, $toIndex)
91
    {
92
        $mappedSettings = ['sourceOrder' => [], 'sources' => []];
93
94
        if (isset($settings['sourceOrder'])) {
95
            foreach ($settings['sourceOrder'] as $row) {
96
                if ($row[0] == 'key') {
97
                    $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...
98
                }
99
                $mappedSettings['sourceOrder'][] = $row;
100 2
            }
101
        }
102 2
103 2
        if (isset($settings['sources'])) {
104 2
            foreach ($settings['sources'] as $source => $sourceSettings) {
105 2
                $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...
106 2
                $mappedSettings['sources'][$mappedSource] = [
107 2
                  '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...
108 2
                ];
109 2
            }
110 2
        }
111 2
112 2
        return $mappedSettings;
113 2
    }
114
}
115