CollectionAdapter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 21.18 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 18
loc 85
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A ensureIndex() 0 4 1
A insert() 9 9 2
A update() 9 9 2
A remove() 0 9 2
A find() 0 6 1
A findOne() 0 15 2
A drop() 0 5 1
A count() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Apix\Cache\Mongo;
4
5
use MongoDB\Collection;
6
7
/**
8
 * allows to use \MongoDB\Collection in the code as \MongoCollection
9
 */
10
class CollectionAdapter /*extends \MongoCollection*/
11
{
12
    /**
13
     * @var Collection
14
     */
15
    private $collection;
16
17
    /**
18
     * CollectionAdapter constructor.
19
     * @param Collection $collection
20
     */
21
    public function __construct(Collection $collection)
22
    {
23
        $this->collection = $collection;
24
    }
25
26
    public function ensureIndex(array $keys, array $options = array())
27
    {
28
        $this->collection->createIndex($keys, $options);
29
    }
30
31 View Code Duplication
    public function insert($a, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        try {
34
            $this->collection->insertOne($a, $options);
35
            return ['ok' => 1];
36
        } catch (\Exception $e) {
37
            return ['ok' => 0, 'error' => $e->getMessage()];
38
        }
39
    }
40
41 View Code Duplication
    public function update(array $criteria, array $newobj, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        try {
44
            $this->collection->updateOne($criteria, array('$set' => $newobj), $options);
45
            return ['ok' => 1];
46
        } catch (\Exception $e) {
47
            return ['ok' => 0, 'error' => $e->getMessage()];
48
        }
49
    }
50
51
    public function remove(array $criteria = array(), array $options = array())
52
    {
53
        try {
54
            $result = $this->collection->deleteMany($criteria, $options);
55
            return ['ok' => 1, 'n' => $result->getDeletedCount()];
56
        } catch (\Exception $e) {
57
            return ['ok' => 0, 'error' => $e->getMessage()];
58
        }
59
    }
60
61
    public function find(array $query = array(), array $fields = array())
62
    {
63
        $options = ['projection' => array_fill_keys($fields, 1) + ['_id' => 0]];
64
65
        return $this->collection->find($query, $options);
66
    }
67
68
    public function findOne(array $query = array(), array $fields = array())
69
    {
70
        $options = ['projection' => array_fill_keys($fields, 1) + ['_id' => 0]];
71
72
        $result =  $this->collection->findOne($query, $options);
73
74
        // mimic \MongoDate->sec
75
        if (!empty($result['expire'])) {
76
            $sec = $result['expire']->toDateTime()->getTimestamp();
77
            $result['expire'] = new \stdClass();
78
            $result['expire']->sec = $sec;
79
        }
80
81
        return $result;
82
    }
83
84
    public function drop()
85
    {
86
        $this->collection->drop();
87
        return ['ok' => 1];
88
    }
89
90
    public function count($query = array())
91
    {
92
        return $this->collection->count($query);
93
    }
94
}