Model::install()   D
last analyzed

Complexity

Conditions 9
Paths 30

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 32
rs 4.909
cc 9
eloc 22
nc 30
nop 2
1
<?php
2
3
namespace ModelUtils;
4
5
class Model extends ModelUtils
6
{
7
    public $schema = [];
8
    public $type = "basic"; // Possible options are basic, cache, search
9
    public $collectionName = null;
10
    public $dataFile = null;
11
    public $schemaConfig = null;
12
13
14
    public function __construct()
15
    {
16
        $this->create();
17
    }
18
19
    public function create()
20
    {
21
        $this->schema = parse_ini_string(trim($this->schemaConfig), true);
22
        $this->dataFile = (isset($config['data_file'])) ? $config['data_file'] : null;
0 ignored issues
show
Bug introduced by
The variable $config seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
23
    }
24
25
    public function validate($doc)
26
    {
27
        return $this->validateDoc($this->schema, $doc);
28
    }
29
30
    public function setDefaults($doc)
31
    {
32
        return $this->setModelDefaults($this->schema, $doc);
33
    }
34
35
36
    public function fitDoc($doc)
37
    {
38
        return $this->fitDocToModel($this->schema, $doc);
39
    }
40
41
    public function install($dbConn, $baseDir)
42
    {
43
        $dbConn->drop($this->collectionName, $this->schema);
44
        $dbConn->create($this->collectionName, $this->schema);
45
        $indexes = [];
46
        foreach ($this->schema as $field => $fconfig) {
47
            if ($fconfig['_index'] === true) {
48
                $index = ['key'=>[$field=>1]];
49
                if (isset($fconfig["_index_type"])) {
50
                    switch ($fconfig["_index_type"]) {
51
                        case 'unique':
52
                            $index['unique'] = true;
53
                            break;
54
                    }
55
                }
56
                $indexes[] = $index;
57
            }
58
        }
59
        if ($this->dataFile !== null) {
60
            if (file_exists($baseDir.$this->dataFile)) {
61
                $data = json_decode(file_get_contents($baseDir.$this->dataFile), true);
62
                foreach ($data as $item) {
63
                    $item = $this->setModelDefaults($this->schema, $item);
64
                    $doc = $this->validateDoc($this->schema, $item);
65
                    $dbConn->insert($this->collectionName, $doc);
66
                }
67
            }
68
        }
69
        if (count($indexes)>0) {
70
            $dbConn->createIndexes($this->collectionName, $indexes);
71
        }
72
    }
73
}
74