Passed
Push — master ( f47e0d...d9327a )
by Simon
10:55 queued 52s
created

FluentSchemaExtension::onAfterFieldDefinition()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 9.6111
1
<?php
2
3
4
namespace Firesphere\SolrFluent\Extensions;
5
6
7
use Firesphere\SolrSearch\Services\SchemaService;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\ORM\ArrayList;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\ORM\DataObject;
12
use TractorCow\Fluent\Model\Locale;
13
14
/**
15
 * Class FluentSchemaExtension
16
 * @package Firesphere\SolrFluent\Extensions
17
 * @property SchemaService|FluentSchemaExtension $owner
18
 */
19
class FluentSchemaExtension extends Extension
20
{
21
22
    /**
23
     * Add the locale fields
24
     *
25
     * @param ArrayList|DataList $data
26
     * @param DataObject $item
27
     */
28
    public function onAfterFieldDefinition($data, $item): void
29
    {
30
        $locales = Locale::get();
31
32
        foreach ($locales as $locale) {
33
            $isDest = strpos($item['Destination'], $locale->Locale);
34
            if (($isDest === 0 || $item['Destination'] === null) && $locale->Locale !== null) {
35
                $copy = $item;
36
                $copy['Field'] = $item['Field'] . '_' . $locale->Locale;
37
                $data->push($copy);
38
            }
39
        }
40
    }
41
}
42