tryEagerLoadingRelation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Gurucomkz\EagerLoading;
4
5
use SilverStripe\ORM\ArrayList;
6
7
/**
8
 * Use this trait to allow access to the eager-loaded has_many & many_many fields.
9
 *
10
 */
11
trait EagerLoaderMultiAccessor
12
{
13 7
    public function __call($method, $arguments)
14
    {
15 7
        if ($method !== 'tryEagerLoadingRelation') {
16 7
            if (null !== ($eagerResult = $this->tryEagerLoadingRelation($method))) {
17 5
                return $eagerResult;
18
            }
19
        }
20 2
        return parent::__call($method, $arguments);
21
    }
22
23
    public $eagerLoadingCache = [];
24
25 7
    public function addEagerRelation($relationName, $content)
26
    {
27 7
        if (!isset($this->eagerLoadingCache)) {
28
            $this->eagerLoadingCache = [];
29
        }
30 7
        $this->eagerLoadingCache[$relationName] = ArrayList::create($content);
31
    }
32
33
    /**
34
     * must be called from
35
     *
36
     * @param string $method
37
     * @param array $arguments
38
     * @return Object|void Void when nothing found
39
     */
40 7
    public function tryEagerLoadingRelation($method)
41
    {
42 7
        if (!isset($this->eagerLoadingCache[$method])) {
43 2
            return null;
44
        }
45
46 5
        return $this->eagerLoadingCache[$method];
47
    }
48
}
49