NOSQLParserTrait::initConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
namespace NOSQL\Models\base;
3
4
use MongoDB\Database;
5
use NOSQL\Dto\CollectionDto;
6
use NOSQL\Dto\IndexDto;
7
use NOSQL\Dto\PropertyDto;
8
use NOSQL\Exceptions\NOSQLParserException;
9
use NOSQL\Models\NOSQLActiveRecord;
10
use NOSQL\Services\NOSQLService;
11
use NOSQL\Services\ParserService;
12
use PSFS\base\Cache;
13
use PSFS\base\types\helpers\GeneratorHelper;
14
15
/**
16
 * Trait NOSQLParserTrait
17
 * @package NOSQL\Models\base
18
 */
19
trait NOSQLParserTrait {
20
    protected $domain;
21
    /**
22
     * @var CollectionDto
23
     */
24
    protected $schema;
25
26
    /**
27
     * @return mixed
28
     */
29
    public function getDomain()
30
    {
31
        return $this->domain;
32
    }
33
34
    /**
35
     * @param mixed $domain
36
     */
37
    public function setDomain($domain)
38
    {
39
        $this->domain = $domain;
40
    }
41
42
    /**
43
     * @return CollectionDto
44
     */
45
    public function getSchema()
46
    {
47
        return $this->schema;
48
    }
49
50
    /**
51
     * @param CollectionDto $schema
52
     */
53
    public function setSchema(CollectionDto $schema)
54
    {
55
        $this->schema = $schema;
56
    }
57
58
    /**
59
     * @param PropertyDto $property
60
     */
61
    public function addProperty(PropertyDto $property) {
62
        $this->schema->properties[] = $property;
63
    }
64
65
    /**
66
     * @param IndexDto $index
67
     */
68
    public function addIndex(IndexDto $index) {
69
        $this->schema->indexes[] = $index;
70
    }
71
72
    /**
73
     * @throws NOSQLParserException
74
     * @throws \PSFS\base\exception\GeneratorException
75
     */
76
    protected function hydrate() {
77
        if(empty($this->domain)) {
78
            throw new NOSQLParserException(t('Domain not defined'), NOSQLParserException::NOSQL_PARSER_DOMAIN_NOT_DEFINED);
0 ignored issues
show
Bug introduced by
NOSQL\Exceptions\NOSQLPa...RSER_DOMAIN_NOT_DEFINED of type string is incompatible with the type integer expected by parameter $code of NOSQL\Exceptions\NOSQLPa...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            throw new NOSQLParserException(t('Domain not defined'), /** @scrutinizer ignore-type */ NOSQLParserException::NOSQL_PARSER_DOMAIN_NOT_DEFINED);
Loading history...
79
        }
80
        $paths = NOSQLService::getDomainPaths($this->domain);
81
        $schemaFilename = $paths['base'] . 'Config' . DIRECTORY_SEPARATOR . 'schema.json';
82
        if(file_exists($schemaFilename)) {
83
            $schema = Cache::getInstance()->getDataFromFile($schemaFilename, Cache::JSON, true);
84
            $class = get_called_class();
85
            $this->schema = new CollectionDto(false);
86
            foreach($schema as $collection) {
87
                $collectionName = $collection['name'];
88
                if(false !== strpos($class, $collectionName)) {
89
                    $this->schema->fromArray($collection);
90
                    break;
91
                }
92
            }
93
        } else {
94
            throw  new NOSQLParserException(t('Schema file not exists'), NOSQLParserException::NOSQL_PARSER_SCHEMA_NOT_DEFINED);
95
        }
96
    }
97
98
    /**
99
     * @param Database $con
100
     * @param NOSQLActiveRecord $model
101
     * @return Database
102
     */
103
    public static function initConnection(NOSQLActiveRecord $model, Database $con = null)
104
    {
105
        if (null === $con) {
106
            $con = ParserService::getInstance()->createConnection($model->getDomain());
107
        }
108
        return $con;
109
    }
110
111
}
112