Completed
Push — master ( 6deb59...c98370 )
by James Ekow Abaka
22:47
created

ORMContext::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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