EagerLoaderMultiAccessor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 3
b 0
f 0
dl 0
loc 36
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 8 3
A addEagerRelation() 0 6 2
A tryEagerLoadingRelation() 0 7 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