GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BatchOperation   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
c 5
b 0
f 0
lcom 1
cbo 1
dl 0
loc 114
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 1 1
A add() 0 6 1
B execute() 0 20 5
A getResult() 0 4 1
A count() 0 4 1
B __construct() 0 34 6
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo;
13
14
abstract class BatchOperation implements \Countable
15
{
16
    /**
17
     * Batch operation class name. Must be override in child classes
18
     * @var string
19
     */
20
    protected $batchClass;
21
22
    /**
23
     * @var Collection
24
     */
25
    protected $collection;
26
27
    /**
28
     * Batch operation instance
29
     * @var \MongoWriteBatch
30
     */
31
    private $batch;
32
33
    /**
34
     * Amount of operations in batch operation
35
     * @var int
36
     */
37
    private $counter = 0;
38
39
    /**
40
     * Result of executed batch operation
41
     * @var array
42
     */
43
    protected $result;
44
45
    /**
46
     * @param Collection    $collection
47
     * @param int|string    $writeConcern  Write concern. Default is 1 (Acknowledged).
48
     *                                     More info at http://php.net/manual/ru/mongo.writeconcerns.php
49
     * @param int           $timeout       Timeout for write concern. Default is 10000 milliseconds
50
     * @param bool          $ordered       Set to true if MongoDB must apply this batch in order (sequentially,
51
     *                                     one item at a time) or can rearrange it. Defaults to TRUE
52
     */
53
    public function __construct(
54
        Collection $collection,
55
        $writeConcern = null,
56
        $timeout = null,
57
        $ordered = null
58
    ) {
59
        $this->collection = $collection;
60
61
        $writeOptions = array();
62
63
        if (null !== $writeConcern) {
64
            $writeOptions['w'] = $writeConcern;
65
        }
66
67
        if (null !== $timeout && is_numeric($timeout)) {
68
            if (version_compare(\MongoClient::VERSION, '1.5', '<=')) {
69
                $writeOptions['wtimeout'] = (int) $timeout;
70
            } else {
71
                $writeOptions['wTimeoutMS'] = (int) $timeout;
72
            }
73
        }
74
75
        if (true === $ordered) {
76
            $writeOptions['ordered'] = true;
77
        }
78
79
        $className = $this->batchClass;
80
        $this->batch = new $className(
81
            $this->collection->getMongoCollection(),
82
            $writeOptions
83
        );
84
85
        $this->init();
86
    }
87
88
    protected function init() {}
89
90
    protected function add($data)
91
    {
92
        $this->batch->add($data);
93
        $this->counter++;
94
        return $this;
95
    }
96
97
    public function execute($writeConcern = null, $timeout = null, $ordered = null)
98
    {
99
        $writeOptions = array();
100
101
        if (null !== $writeConcern) {
102
            $writeOptions['w'] = $writeConcern;
103
        }
104
105
        if ($timeout && is_numeric($timeout)) {
106
            $writeOptions['wtimeout'] = (int) $timeout;
107
        }
108
109
        if (true === $ordered) {
110
            $writeOptions['ordered'] = true;
111
        }
112
113
        $this->result = $this->batch->execute($writeOptions);
114
115
        return $this;
116
    }
117
118
    public function getResult()
119
    {
120
        return $this->result;
121
    }
122
123
    public function count()
124
    {
125
        return $this->counter;
126
    }
127
}
128