Model   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 15
c 6
b 0
f 0
lcom 1
cbo 1
dl 0
loc 69
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 5 2
A validate() 0 4 1
A setDefaults() 0 4 1
A fitDoc() 0 4 1
D install() 0 32 9
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