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

Context::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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