Completed
Push — master ( b9b4e1...acec2e )
by James Ekow Abaka
02:32
created

ORMContext   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.48%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 100
ccs 33
cts 41
cp 0.8048
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 3 1
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 6 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
    private $config;
20
    
21 36
    public function __construct(Container $container, array $config) {
22 36
        $this->container = $container;
23 36
        $this->config = $config;
24 36
        $this->dbContext = $container->resolve(DbContext::class, ['config' => $config]);
25 36
        $this->container->setup([
26 36
            interfaces\ModelJoinerInterface::class => Resolver::class,
27
            interfaces\TableNameResolverInterface::class => Resolver::class,
28
            interfaces\ModelClassResolverInterface::class => Resolver::class
29 36
        ], false);
30 36
        $this->cache = $this->container->resolve(Cache::class);
31 36
        self::$instance = $this;
32 36
    }
33
34
    /**
35
     * A helper for loading a method described as a string.
36
     * @param string $path Model name as string 
37
     * @return \nibii\RecordWrapper
38
     * @throws NibiiException
39
     */
40 6
    public function load($path) {
41
        try {
42 6
            $className = $this->getClassName($path);
43 6
            return $this->container->resolve($className);
44
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
45
            throw new
46
            NibiiException("Failed to load model [$path]. The class [$className] could not be found. Ensure that you have properly setup class name resolutions.");
47
        }
48
    }
49
50
    /**
51
     * Returns a class name for junction models needed to perform joint queries.
52
     * @param string $classA
53
     * @param string $classB
54
     * @return string
55
     */
56 4
    public function joinModels($classA, $classB) {
57 4
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
58 4
            ->getJunctionClassName($classA, $classB);
59
    }
60
61
    /**
62
     * @param RecordWrapper $instance
63
     */
64 36
    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...
65 36
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
66 36
            ->getTableName($instance);
67
    }
68
69 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...
70 14
        return $this->container->singleton(interfaces\ModelClassResolverInterface::class)
71 14
            ->getModelClassName($model, $context);
72
    }
73
74
    /**
75
     * @param string $class
76
     */
77 28
    public function getModelName($class) {
78 28
        return $class;
79
    }
80
    
81 36
    public static function getInstance() {
82 36
        if(self::$instance === null){ 
83
            throw new NibiiException("A context has not yet been initialized");
84
        }
85 36
        return self::$instance;
86
    }
87
    
88 36
    public function getContainer() {
89 36
        return $this->container;
90
    }
91
    
92 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...
93 28
        return $this->cache;
94
    }
95
96
    public function getConfig() {
97
        return $this->config;
98
    }
99
    
100
    /**
101
     * 
102
     * @return \ntentan\atiaa\DbContext
103
     */
104 36
    public function getDbContext() {
105 36
        return $this->dbContext;
106
    }
107
    
108
    public function __destruct() {
109
        self::$instance = null;
110
    }
111
112
}
113