Completed
Push — master ( 59402b...9ecfd4 )
by Andreas
12s
created

WriteConcernConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createWriteConcernFromParameters() 0 15 4
A createWriteConcernFromArray() 0 7 3
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|bool $wstring
22
     * @param int $wtimeout
23
     * @return \MongoDB\Driver\WriteConcern
24
     */
25 277
    protected function createWriteConcernFromParameters($wstring, $wtimeout)
26
    {
27
        // Convert legacy write concern
28 277
        if (is_bool($wstring)) {
29 2
            $wstring = (int) $wstring;
30 2
        }
31
32 277
        if (! is_string($wstring) && ! is_int($wstring)) {
33
            trigger_error("w for WriteConcern must be a string or integer", E_USER_WARNING);
34
            return false;
35
        }
36
37
        // Ensure wtimeout is not < 0
38 277
        return new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0));
39
    }
40
41
    /**
42
     * @param array $writeConcernArray
43
     * @return \MongoDB\Driver\WriteConcern
44
     */
45 276
    protected function createWriteConcernFromArray($writeConcernArray)
46
    {
47 276
        $wstring = isset($writeConcernArray['w']) ? $writeConcernArray['w'] : 1;
48 276
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
49
50 276
        return $this->createWriteConcernFromParameters($wstring, $wtimeout);
51
    }
52
}
53