Completed
Push — master ( f80c47...bf55ad )
by James Ekow Abaka
03:07
created

ORMContext::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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
11
 * models.
12
 */
13
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...
14
{
15
16
    private $dbContext;
17
    private static $instance;
18
    private $cache;
19
    private $modelFactory;
20
21
    private function __construct(ModelFactoryInterface $modelFactory, DbContext $dbContext, Cache $cache)
22 36
    {
23
        $this->modelFactory = $modelFactory;
24 36
        $this->dbContext = $dbContext;
25 36
        $this->cache = $cache;
26 36
    }
27 36
28 36
    public static function initialize(ModelFactoryInterface $modelFactory, DbContext $dbContext, Cache $cache) : ORMContext
29
    {
30
        self::$instance = new self($modelFactory, $dbContext, $cache);
31 36
        return self::$instance;
32 36
    }
33 36
34 36
    /**
35
     * A helper for loading a method described as a string.
36
     * @param string $path Model name as string 
37
     * @return \nibii\RecordWrapper
38
     * @throws NibiiException
39
     */
40
    public function load($path)
41
    {
42 6
        try {
43
            return $this->modelFactory->createModel($path, null);
44
        } catch (\ntentan\panie\exceptions\ResolutionException $e) {
45 6
            throw new
46 6
            NibiiException("Failed to load model [$path]. The class [$className] could not be found. Ensure that you have properly setup class name resolutions.");
47
        }
48
    }
49
50
    /**
51
     * Returns a class name for junction models needed to perform joint queries.
52
     * @param string $classA
53
     * @param string $classB
54
     * @return string
55
     */
56
    public function joinModels($classA, $classB)
57
    {
58
        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...
59 4
                        ->getJunctionClassName($classA, $classB);
60
    }
61 4
62 4
    /**
63
     * @param RecordWrapper $instance
64
     */
65
    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...
66
    {
67
        return$this->container->singleton(interfaces\TableNameResolverInterface::class)
68 36
                        ->getTableName($instance);
69
    }
70 36
71 36
    /*public function getClassName($model, $context = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
    {
73
        return $this->container->singleton(interfaces\ModelClassResolverInterface::class)
74 14
                        ->getModelClassName($model, $context);
75
    }*/
76 14
77 14
    /**
78
     * @param string $class
79
     */
80
    public function getModelName($class)
81
    {
82
        return $class;
83 28
    }
84
85 28
    public static function getInstance()
86
    {
87
        if (self::$instance === null) {
88 36
            throw new NibiiException("A context has not yet been initialized");
89
        }
90 36
        return self::$instance;
91
    }
92
93 36
    public function getCache()
94
    {
95
        return $this->cache;
96 36
    }
97
98 36
    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...
99
    {
100
        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...
101 28
    }
102
103 28
    /**
104
     * 
105
     * @return \ntentan\atiaa\DbContext
106
     */
107
    public function getDbContext()
108
    {
109
        return $this->dbContext;
110
    }
111
112
    public function __destruct()
113
    {
114
        self::$instance = null;
115 36
    }
116
117
}
118