Completed
Push — master ( 728703...b64a14 )
by Andreas
13s
created

createWriteConcernFromParameters()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 5
cts 7
cp 0.7143
crap 4.3731
rs 9.2
c 0
b 0
f 0
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 252
    protected function createWriteConcernFromParameters($wstring, $wtimeout)
26
    {
27
        // Convert legacy write concern
28 252
        if (is_bool($wstring)) {
29 2
            $wstring = (int) $wstring;
30
        }
31
32 252
        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 252
        return new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0));
39
    }
40
41
    /**
42
     * @param array $writeConcernArray
43
     * @return \MongoDB\Driver\WriteConcern
44
     */
45 251
    protected function createWriteConcernFromArray($writeConcernArray)
46
    {
47 251
        $wstring = isset($writeConcernArray['w']) ? $writeConcernArray['w'] : 1;
48 251
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
49
50 251
        return $this->createWriteConcernFromParameters($wstring, $wtimeout);
51
    }
52
}
53