Completed
Push — master ( 64514c...d88910 )
by Andreas
11s
created

WriteConcernConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

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