Completed
Pull Request — master (#21)
by
unknown
38:19 queued 30:53
created

WriteConcern   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.24%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 70
ccs 15
cts 17
cp 0.8824
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
setWriteConcern() 0 1 ?
A getWriteConcern() 0 12 2
A createWriteConcernFromParameters() 0 10 3
A setWriteConcernFromParameters() 0 6 1
A setWriteConcernFromArray() 0 7 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 89
    public function getWriteConcern()
39
    {
40 89
        if ($this->writeConcern === null) {
41 88
            $this->writeConcern = new \MongoDB\Driver\WriteConcern(1);
42
        }
43
44
        return [
45 89
            'w' => $this->writeConcern->getW(),
46 89
            '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 89
    protected function createWriteConcernFromParameters($wstring, $wtimeout)
57
    {
58 89
        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 89
        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 89
    protected function setWriteConcernFromParameters($wstring, $wtimeout = 0)
73
    {
74 89
        $this->writeConcern = $this->createWriteConcernFromParameters($wstring, $wtimeout);
75
76 89
        return true;
77
    }
78
79
    /**
80
     * @param array $writeConcernArray
81
     * @return bool
82
     */
83 88
    protected function setWriteConcernFromArray($writeConcernArray)
84
    {
85 88
        $wstring = $writeConcernArray['w'];
86 88
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
87
88 88
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
89
    }
90
}
91