Completed
Push — master ( f22a46...046041 )
by Patrick
02:27 queued 12s
created

MongoDataTable::update()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
1
<?php
2
namespace Flipside\Data;
3
4
class MongoDataTable extends DataTable
5
{
6
    protected $collection;
7
    protected $name;
8
9
    public function __construct($collection, $collection_name = false)
10
    {
11
        $this->collection = $collection;
12
        $this->name = $collection_name;
13
    }
14
15
    public function count($filter = false)
16
    {
17
        $criteria = array();
18
        if($filter !== false)
19
        {
20
            if($filter instanceof \Data\Filter)
0 ignored issues
show
Bug introduced by
The class Data\Filter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
21
            {
22
                $criteria = $filter->to_mongo_filter();
23
            }
24
            else
25
            {
26
                $criteria = $filter;
27
            }
28
        }
29
        return $this->collection->count($criteria, array(), $this->name);
30
    }
31
32
    private function getCriteriaFromFilter($filter)
33
    {
34
        if($filter === false)
35
        {
36
            return array();
37
        }
38
        if(is_array($filter))
39
        {
40
            return $filter;
41
        }
42
        return $filter->to_mongo_filter();
43
    }
44
45
    public function read($filter = false, $select = false, $count = false, $skip = false, $sort = false, $params = false)
46
    {
47
        $fields   = array();
48
        $criteria = $this->getCriteriaFromFilter($filter);
49
        if($select !== false)
50
        {
51
            $fields = array_fill_keys($select, 1);
52
        }
53
        if($params !== false && isset($params['fields']))
54
        {
55
            $fields = array_merge($fields, $params['fields']);
56
        }
57
        $options = array('projection'=>$fields);
58
        if($count !== false)
59
        {
60
            $options['count'] = $count;
61
        }
62
        if($sort !== false)
63
        {
64
            $options['sort'] = $sort;
65
        }
66
        if($skip !== false)
67
        {
68
            $options['skip'] = $skip;
69
        }
70
        $cursor = $this->collection->find($criteria, $options, $this->name);
71
        if(method_exists($cursor, 'setTypeMap'))
72
        {
73
            $cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
74
            return $cursor->toArray();
75
        }
76
        $ret = array();
77
        foreach($cursor as $doc)
78
        {
79
            array_push($ret, $doc);
80
        }
81
        return $ret;
82
    }
83
84
    public function create($data)
85
    {
86
        $res = $this->collection->insert($data, array(), $this->name);
87
        if($res === false || $res['err'] !== null)
88
        {
89
            return false;
90
        }
91
        return $data['_id'];
92
    }
93
94
    public function update($filter, $data)
95
    {
96
        $criteria = $this->getCriteriaFromFilter($filter);
97
        if(!is_array($data))
98
        {
99
            $data = json_decode(json_encode($data), true);
100
        }
101
        if(isset($data['_id']))
102
        {
103
            unset($data['_id']);
104
        }
105
        $res = $this->collection->update($criteria, array('$set' => $data), array(), $this->name);
106
        if($res === false || $res['err'] !== null)
107
        {
108
            return false;
109
        }
110
        return true;
111
    }
112
113
    public function delete($filter)
114
    {
115
        $criteria = $this->getCriteriaFromFilter($filter);
116
        $res = $this->collection->remove($criteria, array(), $this->name);
117
        if($res === false || $res['err'] !== null)
118
        {
119
            return false;
120
        }
121
        return true;
122
    }
123
}
124