Completed
Push — master ( 7fd5f6...af8ac7 )
by James Ekow Abaka
01:40
created

ORMContext::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1.037
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 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
        /*$this->container->bind(interfaces\ModelJoinerInterface::class)
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
                ->to(Resolver::class);
27
        $this->container->bind(interfaces\TableNameResolverInterface::class)
28
                ->to(Resolver::class);
29
        $this->container->bind(interfaces\ModelClassResolverInterface::class)
30
                ->to(Resolver::class);*/
31 37
        $this->cache = $this->container->resolve(Cache::class);
32 37
        self::$instance = $this;
33 37
    }
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
     * @throws NibiiException
40
     */
41 2
    public function load($path) {
42
        try {
43 2
            $className = $this->getClassName($path);
44
            return $this->container->resolve($className);
45 2
        } 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
     * @return string
56
     */
57
    public function joinModels($classA, $classB) {
58
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
59
            ->getJunctionClassName($classA, $classB);
60
    }
61
62
    /**
63
     * @param RecordWrapper $instance
64
     */
65 35
    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 35
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
67 1
            ->getTableName($instance);
68
    }
69
70 2
    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 2
        return $this->container->singleton(interfaces\ModelClassResolverInterface::class)
72
            ->getModelClassName($model, $context);
73
    }
74
75
    /**
76
     * @param string $class
77
     */
78
    public function getModelName($class) {
79
        return $class;
80
    }
81
    
82 35
    public static function getInstance($container = null) {
83 35
        if(self::$instance === null && $container !== null){ 
84
            $container->resolve(self::class);
85 35
        } elseif (self::$instance === null) {
86
            throw new NibiiException("A context has not yet been initialized");
87
        }
88 35
        return self::$instance;
89
    }
90
    
91 35
    public function getContainer() {
92 35
        return $this->container;
93
    }
94
    
95
    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...
96
        return $this->cache;
97
    }
98
99
    public function getConfig() {
100
        return $this->config;
101
    }
102
    
103
    /**
104
     * 
105
     * @return \ntentan\atiaa\DbContext
106
     */
107 1
    public function getDbContext() {
108 1
        return $this->dbContext;
109
    }
110
    
111
    public function __destruct() {
112
        self::$instance = null;
113
    }
114
115
}
116