Completed
Pull Request — master (#20)
by
unknown
02:36
created

WriteConcern::createWriteConcernFromParameters()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

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