Completed
Push — master ( 5a0a25...383c62 )
by Matteo
02:38
created

Collection::drop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Mattbit\Flat;
4
5
use Mattbit\Flat\Document\Document;
6
use Mattbit\Flat\Document\Identifiable;
7
use Mattbit\Flat\Query\Expression\ExpressionInterface;
8
use Mattbit\Flat\Query\Matcher;
9
use Mattbit\Flat\Storage\DocumentStore;
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
     * Construct a new collection.
30
     *
31
     * @param Database      $database
32
     * @param DocumentStore $store
33
     * @param string        $name
34
     */
35
    public function __construct(Database $database, DocumentStore $store, $name)
36
    {
37
        $this->name = $name;
38
        $this->store = $store;
39
        $this->database = $database;
40
        $this->parser = $database->getParser();
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * Drop the collection.
45
     *
46
     * @return bool
47
     */
48
    public function drop()
49
    {
50
        return $this->database->dropCollection($this);
51
    }
52
53
    /**
54
     * Remove all the documents from the collection.
55
     *
56
     * @return bool
57
     */
58
    public function truncate()
59
    {
60
        $this->store->truncate();
61
    }
62
63
    /**
64
     * Insert a new document.
65
     *
66
     * @param Identifiable $document
67
     *
68
     * @return bool
69
     */
70
    public function insert(Identifiable $document)
71
    {
72
        return $this->store->insertDocument($document);
73
    }
74
75
    /**
76
     * Update existing documents.
77
     *
78
     * @param array        $criteria
79
     * @param Identifiable $updated
80
     * @param bool         $multiple
81
     *
82
     * @return int The count of the documents updated.
83
     */
84 View Code Duplication
    public function update($criteria, Identifiable $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...
85
    {
86
        $limit = $multiple ? 1 : null;
87
        $documents = $this->onMatch($criteria, $limit);
88
89
        foreach ($documents as $document) {
90
            $this->store->updateDocument($updated, $multiple);
0 ignored issues
show
Unused Code introduced by
The call to DocumentStore::updateDocument() has too many arguments starting with $multiple.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
        }
92
    }
93
94
    /**
95
     * Remove documents from the collection.
96
     *
97
     * @param mixed $criteria
98
     * @param bool  $multiple
99
     *
100
     * @return int The count of the document deleted.
101
     */
102 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...
103
    {
104
        $limit = $multiple ? 1 : null;
105
106
        $documents = $this->onMatch($criteria, $limit);
107
        
108
        foreach ($documents as $document) {
109
            $this->store->removeDocument($document->getId());
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * Find documents in the collection.
117
     *
118
     * @param array $criteria
119
     *
120
     * @return array The array of results.
121
     */
122
    public function find($criteria)
123
    {
124
        return $this->onMatch($criteria);
125
    }
126
127
    protected function onMatch($criteria, $limit = null)
128
    {
129
        $expression = $this->parser->parse($criteria);
130
        $matcher = $this->newMatcher($expression);
131
132
        $documents = $this->store->scanDocuments([$matcher, 'match'], $limit);
133
134
        return $documents;
135
    }
136
137
    protected function newMatcher(ExpressionInterface $expression)
138
    {
139
        return new Matcher($expression);
140
    }
141
}
142