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

FluentSchemaExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 19
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A onAfterFieldDefinition() 0 10 5
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