Completed
Push — master ( b3f7d4...9578a4 )
by Andreas
02:50
created

WriteConcern::setWriteConcernFromParameters()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
rs 9.4286
cc 3
eloc 6
nc 2
nop 2
crap 3.3332
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
/**
19
 * @internal
20
 */
21
trait WriteConcern
22
{
23
    /**
24
     * @var \MongoDB\Driver\WriteConcern
25
     */
26
    protected $writeConcern;
27
28
    /**
29
     * @param $wstring
30
     * @param int $wtimeout
31
     * @return bool
32
     */
33
    abstract public function setWriteConcern($wstring, $wtimeout = 0);
34
35
    /**
36
     * @return array
37
     */
38 49
    public function getWriteConcern()
39
    {
40 49
        if ($this->writeConcern === null) {
41 48
            $this->writeConcern = new \MongoDB\Driver\WriteConcern(1);
42 48
        }
43
44
        return [
45 49
            'w' => $this->writeConcern->getW(),
46 49
            'wtimeout' => $this->writeConcern->getWtimeout(),
47 49
        ];
48
49
    }
50
51
    /**
52
     * @param string|int $wstring
53
     * @param int $wtimeout
54
     * @return bool
55
     */
56 49
    protected function setWriteConcernFromParameters($wstring, $wtimeout = 0)
57
    {
58 49
        if (! is_string($wstring) && ! is_int($wstring)) {
59
            trigger_error("w for WriteConcern must be a string or integer", E_WARNING);
60
            return false;
61
        }
62
63
        // Ensure wtimeout is not < 0
64 49
        $this->writeConcern = new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0));
65
66 49
        return true;
67
    }
68
69
    /**
70
     * @param array $writeConcernArray
71
     * @return bool
72
     */
73 48
    protected function setWriteConcernFromArray($writeConcernArray)
74
    {
75 48
        $wstring = $writeConcernArray['w'];
76 48
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
77
78 48
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
79
    }
80
}
81