Completed
Pull Request — master (#28)
by Andreas
03:21
created

createWriteConcernFromArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 4
nc 4
nop 1
crap 12
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
namespace Alcaeus\MongoDbAdapter\Helper;
17
18
trait WriteConcernConverter
19
{
20
    /**
21
     * @param string|int $wstring
22
     * @param int $wtimeout
23
     * @return \MongoDB\Driver\WriteConcern
24
     */
25
    protected function createWriteConcernFromParameters($wstring, $wtimeout)
26
    {
27
        if (! is_string($wstring) && ! is_int($wstring)) {
28
            trigger_error("w for WriteConcern must be a string or integer", E_WARNING);
29
            return false;
30
        }
31
32
        // Ensure wtimeout is not < 0
33
        return new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0));
34
    }
35
36
    /**
37
     * @param array $writeConcernArray
38
     * @return \MongoDB\Driver\WriteConcern
39
     */
40 View Code Duplication
    protected function createWriteConcernFromArray($writeConcernArray)
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...
41
    {
42
        $wstring = isset($writeConcernArray['w']) ? $writeConcernArray['w'] : 1;
43
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
44
45
        return $this->createWriteConcernFromParameters($wstring, $wtimeout);
46
    }
47
}
48