Completed
Push — master ( be821e...418a4d )
by James Ekow Abaka
03:23
created

Context   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 82
rs 10
ccs 34
cts 39
cp 0.8718

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A load() 0 8 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\panie\Container;
6
use ntentan\kaikai\Cache;
7
use ntentan\atiaa\DbContext;
8
9
/**
10
 * A collection of utility methods used as helpers for loading
11
 * models.
12
 */
13
class Context {
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
            return$this->container->resolve(self::getClassName($path));
42
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
43
            throw new
44
            NibiiException("Failed to load model [$path]. Please specify a valid database driver.");
45
        }
46
    }
47
48
    /**
49
     * Returns a class name for junction models needed to perform joint queries.
50
     * @param string $classA
51
     * @param string $classB
52
     * @return string
53
     */
54 4
    public function joinModels($classA, $classB) {
55 4
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
56 4
            ->getJunctionClassName($classA, $classB);
57
    }
58
59 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...
60 37
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
61 37
            ->getTableName($instance);
62
    }
63
64 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...
65 14
        return$this->container->singleton(interfaces\ModelClassResolverInterface::class)
66 14
            ->getModelClassName($model, $context);
67
    }
68
69 28
    public function getModelName($class) {
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 28
        return $class;
71
    }
72
    
73 35
    public static function getInstance() {
74 35
        if(self::$instance === null) throw new NibiiException("A context has not yet been initialized");
75 35
        return self::$instance;
76
    }
77
    
78 37
    public function getContainer() {
79 37
        return $this->container;
80
    }
81
    
82 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...
83 28
        return $this->cache;
84
    }
85
    
86 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...
87 37
        return $this->dbContext;
88
    }
89
    
90
    public function __destruct() {
91
        self::$instance = null;
92
    }
93
94
}
95