Passed
Push — master ( 580f23...f2132c )
by Simon
21:50 queued 19:44
created

FluentSchemaExtension::onAfterFieldDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class FluentSchemaExtension|Firesphere\SolrFluent\Extensions\FluentSchemaExtension Add Fluent schema requirements
4
 *
5
 * @package Firesphere\SolrFluent\Extensions
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrFluent\Extensions;
11
12
use Firesphere\SolrSearch\Factories\SchemaFactory;
0 ignored issues
show
Bug introduced by
The type Firesphere\SolrSearch\Factories\SchemaFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverStripe\Core\Extension;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\ORM\DataList;
16
use TractorCow\Fluent\Model\Locale;
17
18
/**
19
 * Class \Firesphere\SolrFluent\Extensions\FluentSchemaExtension
20
 *
21
 * Update the schema with the appropriate locale fields
22
 *
23
 * @package Firesphere\SolrFluent\Extensions
24
 * @property SchemaFactory|FluentSchemaExtension $owner
25
 */
26
class FluentSchemaExtension extends Extension
27
{
28
    /**
29
     * Add the locale fields
30
     *
31
     * @param ArrayList|DataList $data ArrayList to which the copy should be pushed
32
     * @param array $item Item that's going to be altered per locale
33
     */
34 1
    public function onAfterFieldDefinition($data, $item): void
35
    {
36
        // A locale can be null, those need to be skipped
37 1
        $locales = Locale::get()->exclude(['Locale' => null]);
38
39 1
        foreach ($locales as $locale) {
40 1
            $copy = $item;
41 1
            $copy['Field'] = sprintf('%s_%s', $item['Field'], $locale->Locale);
42 1
            $data->push($copy);
43
        }
44 1
    }
45
}
46