DataFilterEagerLoadingExtension::with()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.8333
cc 4
nc 8
nop 1
crap 4
1
<?php
2
namespace Gurucomkz\EagerLoading;
3
4
use SilverStripe\ORM\DataExtension;
5
6
/**
7
 *
8
 * @property-read \SilverStripe\ORM\DataList $owner
9
 *
10
 */
11
class DataFilterEagerLoadingExtension extends DataExtension
12
{
13
14
    public $withList = [];
15
    public $withListOriginal = [];
16 12
    public function with($list)
17
    {
18
        // return $this->owner;
19 12
        if (!isset($this->owner->withList)) {
20 12
            $this->owner->withList = [];
21
        }
22 12
        if (!isset($this->owner->withListOriginal)) {
23 12
            $this->owner->withListOriginal = [];
24
        }
25 12
        if (!is_array($list)) {
26 9
            $list = [$list];
27
        }
28 12
        $this->owner->withListOriginal = $list;
29 12
        $list = array_map(
30 12
            function ($e) {
31 12
                return explode('.', $e);
32 12
            },
33 12
            $list
34 12
        );
35 12
        $this->owner->withList = array_merge($this->owner->withList, $list);
36 12
        return EagerLoadedDataList::cloneFrom($this->owner);
37
    }
38
}
39