Passed
Push — master ( 336aa4...192c12 )
by Fran
02:09
created

NOSQLParserTrait::initConnection()   A

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\ParserService;
11
use PSFS\base\Cache;
12
13
/**
14
 * Trait NOSQLParserTrait
15
 * @package NOSQL\Models\base
16
 */
17
trait NOSQLParserTrait {
18
    protected $domain;
19
    /**
20
     * @var CollectionDto
21
     */
22
    protected $schema;
23
24
    /**
25
     * @return mixed
26
     */
27
    public function getDomain()
28
    {
29
        return $this->domain;
30
    }
31
32
    /**
33
     * @param mixed $domain
34
     */
35
    public function setDomain($domain)
36
    {
37
        $this->domain = $domain;
38
    }
39
40
    /**
41
     * @return CollectionDto
42
     */
43
    public function getSchema()
44
    {
45
        return $this->schema;
46
    }
47
48
    /**
49
     * @param CollectionDto $schema
50
     */
51
    public function setSchema(CollectionDto $schema)
52
    {
53
        $this->schema = $schema;
54
    }
55
56
    /**
57
     * @param PropertyDto $property
58
     */
59
    public function addProperty(PropertyDto $property) {
60
        $this->schema->properties[] = $property;
61
    }
62
63
    /**
64
     * @param IndexDto $index
65
     */
66
    public function addIndex(IndexDto $index) {
67
        $this->schema->indexes[] = $index;
68
    }
69
70
    /**
71
     * @throws NOSQLParserException
72
     * @throws \PSFS\base\exception\GeneratorException
73
     */
74
    protected function hydrate() {
75
        if(empty($this->domain)) {
76
            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

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