Remove AWS SES from feedback email
[busui.git] / lib / rolling-curl / RollingCurlGroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
 
  Authored by Fabian Franz (www.lionsad.de)
  Released under Apache License 2.0
 
$Id$
*/
 
class RollingCurlGroupException extends Exception {}
 
/**
 * @throws RollingCurlGroupException
 */
abstract class RollingCurlGroupRequest extends RollingCurlRequest {
    private $group = null;
 
    /**
     * Set group for this request
     *
     * @param group The group to be set
     */
    function setGroup($group) {
        if (!($group instanceof RollingCurlGroup))
            throw new RollingCurlGroupException("setGroup: group needs to be of instance RollingCurlGroup");
 
        $this->group = $group;
    }
 
    /**
     * Process the request
     *
     *
     */
    function process($output, $info) {
        if ($this->group)
            $this->group->process($output, $info, $this);
    }
 
    /**
     * @return void
     */
    public function __destruct() {
        unset($this->group);
        parent::__destruct();
    }
 
}
 
/**
 * A group of curl requests.
 *
 * @throws RollingCurlGroupException *
 */
class RollingCurlGroup {
    /**
     * @var string group name
     */
    protected $name;
 
    /**
     * @var int total number of requests in a group
     */
    protected $num_requests = 0;
 
    /**
     * @var int total number of finished requests in a group
     */
    protected $finished_requests = 0;
 
    /**
     * @var array requests array
     */
    private $requests = array();
 
    /**
     * @param string $name group name
     * @return void
     */
    function __construct($name) {
        $this->name = $name;
    }
 
    /**
     * @return void
     */
    public function __destruct() {
        unset($this->name, $this->num_requests, $this->finished_requests, $this->requests);
    }
 
    /**
     * Adds request to a group
     *
     * @throws RollingCurlGroupException
     * @param RollingCurlGroupRequest|array $request
     * @return bool
     */
    function add($request) {
        if ($request instanceof RollingCurlGroupRequest) {
            $request->setGroup($this);
            $this->num_requests++;
            $this->requests[] = $request;
        }
        else if (is_array($request)) {
            foreach ($request as $req)
            $this->add($req);
        }
        else
            throw new RollingCurlGroupException("add: Request needs to be of instance RollingCurlGroupRequest");
 
        return true;
    }
 
    /**
     * @throws RollingCurlGroupException
     * @param RollingCurl $rc
     * @return bool
     */
    function addToRC(RollingCurl $rc){
        $ret = true;
 
        while (count($this->requests) > 0){
            $ret1 = $rc->add(array_shift($this->requests));
            if (!$ret1)
                $ret = false;
        }
 
        return $ret;
    }
 
    /**
     * Override to implement custom response processing.
     *
     * Don't forget to call parent::process().
     *
     * @param string $output received page body
     * @param array $info holds various information about response such as HTTP response code, content type, time taken to make request etc.
     * @param RollingCurlRequest $request request used
     * @return void
     */
    function process($output, $info, $request) {
        $this->finished_requests++;
 
        if ($this->finished_requests >= $this->num_requests)
            $this->finished();
    }
 
    /**
     * Override to execute code after all requests in a group are processed.
     *
     * @return void
     */
    function finished() {
    }
 
}
 
/**
 * Group version of rolling curl
 */
class GroupRollingCurl extends RollingCurl {
 
    /**
     * @var mixed common callback for all groups
     */
    private $group_callback = null;
 
    /**
     * @param string $output received page body
     * @param array $info holds various information about response such as HTTP response code, content type, time taken to make request etc.
     * @param RollingCurlRequest $request request used
     * @return void
     */
    protected function process($output, $info, $request) {
        if ($request instanceof RollingCurlGroupRequest)
            $request->process($output, $info);
 
        if (is_callable($this->group_callback))
            call_user_func($this->group_callback, $output, $info, $request);
    }
 
    /**
     * @param mixed $callback common callback for all groups
     * @return void
     */
    function __construct($callback = null) {
        $this->group_callback = $callback;
 
        parent::__construct(array(&$this, "process"));
    }
 
    /**
     * Adds a group to processing queue
     *
     * @param RollingCurlGroup|Request $request
     * @return bool
     */
    public function add($request) {
        if ($request instanceof RollingCurlGroup)
            return $request->addToRC($this);
        else
            return parent::add($request);
    }
 
    /**
     * Execute processing
     *
     * @param int $window_size Max number of simultaneous connections
     * @return bool|string
     */
    public function execute($window_size = null) {
        if (count($this->requests) == 0)
            return false;
 
        return parent::execute($window_size);
    }
}