ModelFetcher::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 12
1
<?php
2
3
namespace BZIon\Twig;
4
5
/**
6
 * A twig global that enables the creation of Models from their IDs
7
 *
8
 * Example usage: `model.newsCategory(15).id`
9
 */
10
class ModelFetcher
11
{
12
    public function __call($type, $arguments)
13
    {
14
        $id = $arguments[0];
15
16
        if ($id instanceof \Model) {
17
            return $id;
18
        }
19
20
        $class = new \ReflectionClass("\\" . ucfirst($type));
21
22
        if (!$class->isSubclassOf("\\Model")) {
23
            throw new \Exception("$type is not a model");
24
        }
25
26
        return $class->getMethod('get')
27
                      ->invoke(null, $id);
28
    }
29
}
30