Completed
Push — master ( 68b359...a3f02e )
by James Ekow Abaka
02:30
created

ORMContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 89
ccs 35
cts 40
cp 0.875
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A load() 0 9 2
A joinModels() 0 4 1
A getModelTable() 0 4 1
A getClassName() 0 4 1
A getModelName() 0 3 1
A getInstance() 0 4 2
A getContainer() 0 3 1
A getCache() 0 3 1
A getDbContext() 0 3 1
A __destruct() 0 3 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\atiaa\DbContext;
6
use ntentan\kaikai\Cache;
7
use ntentan\panie\Container;
8
9
/**
10
 * A collection of utility methods used as helpers for loading
11
 * models.
12
 */
13
class ORMContext {
14
    
15
    private $container;
16
    private $dbContext;
17
    private static $instance;
18
    private $cache;
19
    
20 37
    public function __construct(Container $container) {
21 37
        $this->container = $container;
22 37
        $this->dbContext = $container->resolve(DbContext::class);
23 37
        $this->container->bind(interfaces\ModelJoinerInterface::class)
24 37
                ->to(Resolver::class);
25 37
        $this->container->bind(interfaces\TableNameResolverInterface::class)
26 37
                ->to(Resolver::class);
27 37
        $this->container->bind(interfaces\ModelClassResolverInterface::class)
28 37
                ->to(Resolver::class);
29 37
        $this->cache = $this->container->resolve(Cache::class);
30 37
        self::$instance = $this;
31 37
    }
32
33
    /**
34
     * A helper for loading a method described as a string.
35
     * @param string $path Model name as string 
36
     * @return \nibii\RecordWrapper
37
     * @throws NibiiException
38
     */
39 6
    public function load($path) {
40
        try {
41 6
            $className = $this->getClassName($path);
42 6
            return $this->container->resolve($className);
43
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
44
            throw new
45
            NibiiException("Failed to load model [$path]. The class [$className] could not be found. Ensure that you have properly setup class name resolutions.");
46
        }
47
    }
48
49
    /**
50
     * Returns a class name for junction models needed to perform joint queries.
51
     * @param string $classA
52
     * @param string $classB
53
     * @return string
54
     */
55 4
    public function joinModels($classA, $classB) {
56 4
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
57 4
            ->getJunctionClassName($classA, $classB);
58
    }
59
60
    /**
61
     * @param RecordWrapper $instance
62
     */
63 37
    public function getModelTable($instance) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
64 37
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
65 37
            ->getTableName($instance);
66
    }
67
68 14
    public function getClassName($model, $context = null) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
69 14
        return $this->container->singleton(interfaces\ModelClassResolverInterface::class)
70 14
            ->getModelClassName($model, $context);
71
    }
72
73
    /**
74
     * @param string $class
75
     */
76 28
    public function getModelName($class) {
77 28
        return $class;
78
    }
79
    
80 35
    public static function getInstance() {
81 35
        if(self::$instance === null) throw new NibiiException("A context has not yet been initialized");
82 35
        return self::$instance;
83
    }
84
    
85 37
    public function getContainer() {
86 37
        return $this->container;
87
    }
88
    
89 28
    public function getCache() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90 28
        return $this->cache;
91
    }
92
    
93 37
    public function getDbContext() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
94 37
        return $this->dbContext;
95
    }
96
    
97
    public function __destruct() {
98
        self::$instance = null;
99
    }
100
101
}
102