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
|
|
|
use WriteConcernRaw; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \MongoDB\Driver\WriteConcern |
27
|
|
|
*/ |
28
|
|
|
protected $writeConcern; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $wstring |
32
|
|
|
* @param int $wtimeout |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
abstract public function setWriteConcern($wstring, $wtimeout = 0); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
120 |
|
public function getWriteConcern() |
41
|
|
|
{ |
42
|
120 |
|
if ($this->writeConcern === null) { |
43
|
119 |
|
$this->writeConcern = new \MongoDB\Driver\WriteConcern(1); |
44
|
119 |
|
} |
45
|
|
|
|
46
|
|
|
return [ |
47
|
120 |
|
'w' => $this->writeConcern->getW(), |
48
|
120 |
|
'wtimeout' => $this->writeConcern->getWtimeout(), |
49
|
120 |
|
]; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|int $wstring |
55
|
|
|
* @param int $wtimeout |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
120 |
|
protected function setWriteConcernFromParameters($wstring, $wtimeout = 0) |
59
|
|
|
{ |
60
|
120 |
|
$this->writeConcern = $this->createWriteConcernFromParameters($wstring, $wtimeout); |
61
|
|
|
|
62
|
120 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $writeConcernArray |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
119 |
View Code Duplication |
protected function setWriteConcernFromArray($writeConcernArray) |
|
|
|
|
70
|
|
|
{ |
71
|
119 |
|
$wstring = $writeConcernArray['w']; |
72
|
119 |
|
$wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0; |
73
|
|
|
|
74
|
119 |
|
return $this->setWriteConcernFromParameters($wstring, $wtimeout); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.