Completed
Pull Request — master (#78)
by Andreas
20:53 queued 18:19
created

createWriteConcernFromParameters()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
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
    protected function createWriteConcernFromArray($writeConcernArray)
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