Completed
Push — master ( 87acb3...e1b1f8 )
by Andreas
02:38
created

WriteConcern::getWriteConcern()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2
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 112
    public function getWriteConcern()
39
    {
40 112
        if ($this->writeConcern === null) {
41 111
            $this->writeConcern = new \MongoDB\Driver\WriteConcern(1);
42
        }
43
44
        return [
45 112
            'w' => $this->writeConcern->getW(),
46 112
            'wtimeout' => $this->writeConcern->getWtimeout(),
47
        ];
48
49
    }
50
51
    /**
52
     * @param string|int $wstring
53
     * @param int $wtimeout
54
     * @return \MongoDB\Driver\WriteConcern
55
     */
56 112
    protected function createWriteConcernFromParameters($wstring, $wtimeout)
57
    {
58 112
        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 112
        return new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0));
65
    }
66
67
    /**
68
     * @param string|int $wstring
69
     * @param int $wtimeout
70
     * @return bool
71
     */
72 112
    protected function setWriteConcernFromParameters($wstring, $wtimeout = 0)
73
    {
74 112
        $this->writeConcern = $this->createWriteConcernFromParameters($wstring, $wtimeout);
75
76 112
        return true;
77
    }
78
79
    /**
80
     * @param array $writeConcernArray
81
     * @return bool
82
     */
83 111
    protected function setWriteConcernFromArray($writeConcernArray)
84
    {
85 111
        $wstring = $writeConcernArray['w'];
86 111
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
87
88 111
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
89
    }
90
}
91