Passed
Push — main ( 9cb66d...0deb5f )
by Sergey
02:19
created

EagerLoaderMultiAccessor::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 3
nop 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
    public function __call($method, $arguments)
13
    {
14
        if ($method !== 'tryEagerLoadingRelation'){
15
            if (null !== ($eagerResult = $this->tryEagerLoadingRelation($method))) {
16
                return $eagerResult;
17
            }
18
        }
19
        return parent::__call($method, $arguments);
20
    }
21
22
    public $_eagerLoadingCache = [];
23
24
    public function addEagerRelation($relationName, $content)
25
    {
26
        if (!isset($this->_eagerLoadingCache)) {
27
            $this->_eagerLoadingCache = [];
28
        }
29
        $this->_eagerLoadingCache[$relationName] = ArrayList::create($content);
30
    }
31
32
    /**
33
     * must be called from
34
     *
35
     * @param string $method
36
     * @param array $arguments
37
     * @return Object|void Void when nothing found
38
     */
39
    public function tryEagerLoadingRelation($method)
40
    {
41
        if (!isset($this->_eagerLoadingCache[$method])) {
42
            return null;
43
        }
44
45
        return $this->_eagerLoadingCache[$method];
46
    }
47
}
48