Completed
Push — master ( d427e3...a30571 )
by James Ekow Abaka
01:47
created

ORMContext::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.1481
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\atiaa\DbContext;
6
use ntentan\kaikai\Cache;
7
use ntentan\nibii\interfaces\ModelFactoryInterface;
8
use ntentan\nibii\interfaces\DriverAdapterFactoryInterface;
9
use ntentan\nibii\interfaces\ValidatorFactoryInterface;
10
11
/**
12
 * A collection of utility methods used as helpers for loading models.
13
 */
14
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...
15
{
16
17
    private static $instance;
18
    private $cache;
19
    private $modelFactory;
20
    private $modelValidatorFactory;
21
    private $driverAdapterFactory;
22 36
23
    private function __construct(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache)
24 36
    {
25 36
        $this->modelFactory = $modelFactory;
26 36
        $this->cache = $cache;
27 36
        $this->driverAdapterFactory = $driverAdapterFactory;
28 36
        $this->modelValidatorFactory = $modelValidatorFactory;
29
    }
30
31 36
    public static function initialize(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache): ORMContext
32 36
    {
33 36
        self::$instance = new self($modelFactory, $driverAdapterFactory, $modelValidatorFactory, $cache);
34 36
        return self::$instance;
35
    }
36
37
    /**
38
     * A helper for loading a method described as a string.
39
     * @param string $path Model name as string 
40
     * @return \nibii\RecordWrapper
41
     * @throws NibiiException
42 6
     */
43
    public function load($path)
44
    {
45 6
        try {
46 6
            return $this->modelFactory->createModel($path, null);
47
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
48
            throw new
49
            NibiiException("Failed to load model [$path]. The class [$className] could not be found.");
50
        }
51
    }
52
53
    /**
54
     * Returns a class name for junction models needed to perform joint queries.
55
     * @param string $classA
56
     * @param string $classB
57
     * @return string
58
     */
59 4
    public function joinModels($classA, $classB)
60
    {
61 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...
62 4
                ->getJunctionClassName($classA, $classB);
63
    }
64
65
    /**
66
     * @param RecordWrapper $instance
67
     */
68 36
    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...
69
    {
70 36
        return $this->modelFactory->getModelTable($instance);
71 36
    }
72
73
    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...
74 14
    {
75
        return $this->driverAdapterFactory->createDriverAdapter();
76 14
    }
77 14
78
    /**
79
     * @param string $class
80
     */
81
    public function getModelName($class)
82
    {
83 28
        return $class;
84
    }
85 28
86
    public static function getInstance()
87
    {
88 36
        if (self::$instance === null) {
89
            throw new NibiiException("A context has not yet been initialized");
90 36
        }
91
        return self::$instance;
92
    }
93 36
94
    public function getCache()
95
    {
96 36
        return $this->cache;
97
    }
98 36
99
    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...
100
    {
101 28
        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...
102
    }
103 28
104
    public function getModelDescription($model)
105
    {
106
        return new ModelDescription($model);
107
    }
108
    
109
    public function getModelValidatorFactory() : ValidatorFactoryInterface
110
    {
111
        return $this->modelValidatorFactory;
112
    }
113
    
114
    public function getModelFactory() : ModelFactoryInterface
115 36
    {
116
        return $this->modelFactory;
117 36
    }
118
119
    /**
120
     * 
121
     * @return \ntentan\atiaa\DbContext
122
     */
123
    public function getDbContext()
124
    {
125
        return DbContext::getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<ntentan\atiaa\DbContext>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
    }
127
128
    public function __destruct()
129
    {
130
        self::$instance = null;
131
    }
132
133
}
134