Completed
Push — master ( 9659ff...1e647a )
by Matteo
02:39
created

Collection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 14.38 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 76.32%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 4
dl 21
loc 146
ccs 29
cts 38
cp 0.7632
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A drop() 0 4 1
A truncate() 0 4 1
A insert() 0 4 1
A update() 9 9 3
A remove() 12 12 3
A find() 0 4 1
A count() 0 4 1
A onMatch() 0 9 1
A newMatcher() 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 Mattbit\Flat;
4
5
use Mattbit\Flat\Query\Parser;
6
use Mattbit\Flat\Query\Matcher;
7
use Mattbit\Flat\Storage\DocumentStore;
8
use Mattbit\Flat\Model\DocumentInterface;
9
use Mattbit\Flat\Query\Expression\ExpressionInterface;
10
11
class Collection
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $name;
17
18
    /**
19
     * @var DocumentStore
20
     */
21
    protected $store;
22
23
    /**
24
     * @var Database
25
     */
26
    protected $database;
27
28
    /**
29
     * @var Parser
30
     */
31
    protected $parser;
32
33
    /**
34
     * Construct a new collection.
35
     *
36
     * @param Database      $database
37
     * @param DocumentStore $store
38
     * @param string        $name
39
     */
40 7
    public function __construct(Database $database, DocumentStore $store, $name)
41
    {
42 7
        $this->name = $name;
43 7
        $this->store = $store;
44 7
        $this->database = $database;
45 7
        $this->parser = $database->getParser();
46 7
    }
47
48
    /**
49
     * Drop the collection.
50
     *
51
     * @return bool
52
     */
53 1
    public function drop()
54
    {
55 1
        return $this->database->dropCollection($this->name);
56
    }
57
58
    /**
59
     * Remove all the documents from the collection.
60
     *
61
     * @return bool
62
     */
63 1
    public function truncate()
64
    {
65 1
        $this->store->truncate();
66 1
    }
67
68
    /**
69
     * Insert a new document.
70
     *
71
     * @param DocumentInterface $document
72
     *
73
     * @return bool
74
     */
75 1
    public function insert(DocumentInterface $document)
76
    {
77 1
        return $this->store->insert($document);
78
    }
79
80
    /**
81
     * Update existing documents.
82
     *
83
     * @param array        $criteria
84
     * @param DocumentInterface $updated
85
     * @param bool         $multiple
86
     *
87
     * @return int The count of the documents updated.
88
     */
89 View Code Duplication
    public function update($criteria, DocumentInterface $updated, $multiple = false)
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...
90
    {
91
        $limit = $multiple ? 1 : null;
92
        $documents = $this->onMatch($criteria, $limit);
93
94
        foreach ($documents as $document) {
95
            $this->store->update($updated);
96
        }
97
    }
98
99
    /**
100
     * Remove documents from the collection.
101
     *
102
     * @param mixed $criteria
103
     * @param bool  $multiple
104
     *
105
     * @return int The count of the document deleted.
106
     */
107 1 View Code Duplication
    public function remove($criteria, $multiple = false)
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...
108
    {
109 1
        $limit = $multiple ? 1 : null;
110
111 1
        $documents = $this->onMatch($criteria, $limit);
112
113 1
        foreach ($documents as $document) {
114 1
            $this->store->remove($document->get('_id'));
115 1
        }
116
117 1
        return true;
118
    }
119
120
    /**
121
     * Find documents in the collection.
122
     *
123
     * @param array $criteria
124
     *
125
     * @return array The array of results.
126
     */
127 2
    public function find($criteria = [])
128
    {
129 2
        return $this->onMatch($criteria);
130
    }
131
132
    /**
133
     * Count documents in the collection.
134
     *
135
     * @return int The number of documents.
136
     */
137
    public function count()
138
    {
139
        return $this->store->count();
140
    }
141
142 3
    protected function onMatch($criteria, $limit = null)
143
    {
144 3
        $expression = $this->parser->parse($criteria);
145 3
        $matcher = $this->newMatcher($expression);
146
147 3
        $documents = $this->store->scan([$matcher, 'match'], $limit);
148
149 3
        return $documents;
150
    }
151
152 3
    protected function newMatcher(ExpressionInterface $expression)
153
    {
154 3
        return new Matcher($expression);
155
    }
156
}
157