Completed
Push — master ( bf55ad...1b9783 )
by James Ekow Abaka
01:52
created

ORMContext::getModelDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 models.
11
 */
12
class ORMContext
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
13
{
14
15
    private $dbContext;
16
    private static $instance;
17
    private $cache;
18
    private $modelFactory;
19
    private $driverAdapterFactory;
20
21
    private function __construct(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, DbContext $dbContext, Cache $cache)
22 36
    {
23
        $this->modelFactory = $modelFactory;
24 36
        $this->dbContext = $dbContext;
25 36
        $this->cache = $cache;
26 36
        $this->driverAdapterFactory = $driverAdapterFactory;
27 36
    }
28 36
29
    public static function initialize(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, DbContext $dbContext, Cache $cache) : ORMContext
30
    {
31 36
        self::$instance = new self($modelFactory, $driverAdapterFactory, $dbContext, $cache);
32 36
        return self::$instance;
33 36
    }
34 36
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
    public function load($path)
42 6
    {
43
        try {
44
            return $this->modelFactory->createModel($path, null);
45 6
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
46 6
            throw new
47
            NibiiException("Failed to load model [$path]. The class [$className] could not be found.");
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
    {
59 4
        return$this->container->singleton(interfaces\ModelJoinerInterface::class)
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
                        ->getJunctionClassName($classA, $classB);
61 4
    }
62 4
63
    /**
64
     * @param RecordWrapper $instance
65
     */
66
    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...
67
    {
68 36
        return $this->modelFactory->getModelTable($instance);
69
    }
70 36
71 36
    public function getDriverAdapter()
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...
72
    {
73
        return $this->driverAdapterFactory->createDriverAdapter();
74 14
    }
75
76 14
    /**
77 14
     * @param string $class
78
     */
79
    public function getModelName($class)
80
    {
81
        return $class;
82
    }
83 28
84
    public static function getInstance()
85 28
    {
86
        if (self::$instance === null) {
87
            throw new NibiiException("A context has not yet been initialized");
88 36
        }
89
        return self::$instance;
90 36
    }
91
92
    public function getCache()
93 36
    {
94
        return $this->cache;
95
    }
96 36
97
    public function getConfig()
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...
98 36
    {
99
        return $this->config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
    }
101 28
102
    public function getModelDescription($model)
103 28
    {
104
        return new ModelDescription($model);
105
    }
106
107
    /**
108
     * 
109
     * @return \ntentan\atiaa\DbContext
110
     */
111
    public function getDbContext()
112
    {
113
        return $this->dbContext;
114
    }
115 36
116
    public function __destruct()
117 36
    {
118
        self::$instance = null;
119
    }
120
121
}
122