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
|
|
|
trait WriteConcernRaw |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param string|int $wstring |
22
|
|
|
* @param int $wtimeout |
23
|
|
|
* @return \MongoDB\Driver\WriteConcern |
24
|
|
|
*/ |
25
|
120 |
|
protected function createWriteConcernFromParameters($wstring, $wtimeout) |
26
|
|
|
{ |
27
|
120 |
|
if (! is_string($wstring) && ! is_int($wstring)) { |
28
|
|
|
trigger_error("w for WriteConcern must be a string or integer", E_WARNING); |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Ensure wtimeout is not < 0 |
33
|
120 |
|
return new \MongoDB\Driver\WriteConcern($wstring, max($wtimeout, 0)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $writeConcernArray |
38
|
|
|
* @return \MongoDB\Driver\WriteConcern |
39
|
|
|
*/ |
40
|
6 |
View Code Duplication |
protected function createWriteConcernFromArray($writeConcernArray) |
|
|
|
|
41
|
|
|
{ |
42
|
6 |
|
$wstring = isset($writeConcernArray['w']) ? $writeConcernArray['w'] : 1; |
43
|
6 |
|
$wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0; |
44
|
|
|
|
45
|
6 |
|
return $this->createWriteConcernFromParameters($wstring, $wtimeout); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
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.