Completed
Push — master ( 6bc7cf...be821e )
by James Ekow Abaka
05:06
created

Context   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 6
cts 36
cp 0.1666

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 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 getDbContext() 0 3 1
A __destruct() 0 3 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\panie\Container;
6
7
/**
8
 * A collection of utility methods used as helpers for loading
9
 * models.
10
 */
11
class Context {
12
    
13
    private $container;
14
    private $dbContext;
15
    private static $instance;
16
    
17 36
    public function __construct(Container $container) {
18 36
        $this->container = $container;
19 36
        $this->dbContext = $container->resolve(\ntentan\atiaa\DbContext::class);
20
        $this->container->bind(interfaces\ModelJoinerInterface::class)
21
             ->to(Resolver::class);
22
        $this->container->bind(interfaces\TableNameResolverInterface::class)
23
             ->to(Resolver::class);
24
        $this->container->bind(interfaces\ModelClassResolverInterface::class)
25
             ->to(Resolver::class);
26
        self::$instance = $this;
27
    }
28
29
    /**
30
     * A helper for loading a method described as a string.
31
     * @param string $path Model name as string 
32
     * @return \nibii\RecordWrapper
33
     * @throws NibiiException
34
     */
35
    public function load($path) {
36
        try {
37
            return$this->container->resolve(self::getClassName($path));
38
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
39
            throw new
40
            NibiiException("Failed to load model [$path]. Please specify a valid database driver.");
41
        }
42
    }
43
44
    /**
45
     * Returns a class name for junction models needed to perform joint queries.
46
     * @param string $classA
47
     * @param string $classB
48
     * @return string
49
     */
50
    public function joinModels($classA, $classB) {
51
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
52
            ->getJunctionClassName($classA, $classB);
53
    }
54
55
    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...
56
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
57
            ->getTableName($instance);
58
    }
59
60
    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...
61
        return$this->container->singleton(interfaces\ModelClassResolverInterface::class)
62
            ->getModelClassName($model, $context);
63
    }
64
65
    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...
66
        return $class;
67
    }
68
    
69
    public static function getInstance() {
70
        if(self::$instance === null) throw new NibiiException("A context has not yet been initialized");
71
        return self::$instance;
72
    }
73
    
74
    public function getContainer() {
75
        return $this->container;
76
    }
77
    
78
    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...
79
        return $this->dbContext;
80
    }
81
    
82 36
    public function __destruct() {
83 36
        self::$instance = null;
84 36
    }
85
86
}
87