PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.37
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.37
9.1.3 9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 All 222 releases
wpvr / vendor / posthog / posthog-php / lib / QueueConsumer.php

QueueConsumer.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 8.5.37, at vendor/posthog/posthog-php/lib/QueueConsumer.php

147 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PostHog;
4
5 abstract class QueueConsumer extends Consumer
6 {
7 protected $type = "QueueConsumer";
8
9 protected $queue;
10 protected $max_queue_size = 1000;
11 protected $batch_size = 100;
12 protected $maximum_backoff_duration = 10000; // Set maximum waiting limit to 10s
13 protected $host = "app.posthog.com";
14 protected $compress_request = false;
15
16 /**
17 * Store our api key and options as part of this consumer
18 * @param string $apiKey
19 * @param array $options
20 */
21 public function __construct($apiKey, $options = array())
22 {
23 parent::__construct($apiKey, $options);
24
25 if (isset($options["max_queue_size"])) {
26 $this->max_queue_size = $options["max_queue_size"];
27 }
28
29 if (isset($options["batch_size"])) {
30 $this->batch_size = $options["batch_size"];
31 }
32
33 if (isset($options["maximum_backoff_duration"])) {
34 $this->maximum_backoff_duration = (int) $options["maximum_backoff_duration"];
35 }
36
37 if (isset($options["host"])) {
38 $this->host = $options["host"];
39
40 if ($this->host && preg_match("/^https?:\\/\\//i", $this->host)) {
41 $this->options['ssl'] = substr($this->host, 0, 5) == 'https';
42 $this->host = preg_replace("/^https?:\\/\\//i", "", $this->host);
43 }
44 }
45
46 if (isset($options["compress_request"])) {
47 $this->compress_request = json_decode($options["compress_request"]);
48 }
49
50 $this->queue = array();
51 }
52
53 public function __destruct()
54 {
55 // Flush our queue on destruction
56 $this->flush();
57 }
58
59 /**
60 * Captures a user action
61 *
62 * @param array $message
63 * @return boolean whether the capture call succeeded
64 */
65 public function capture(array $message)
66 {
67 return $this->enqueue($message);
68 }
69
70 /**
71 * Tags properties about the user.
72 *
73 * @param array $message
74 * @return boolean whether the identify call succeeded
75 */
76 public function identify(array $message)
77 {
78 return $this->enqueue($message);
79 }
80
81 /**
82 * Aliases from one user id to another
83 *
84 * @param array $message
85 * @return boolean whether the alias call succeeded
86 */
87 public function alias(array $message)
88 {
89 return $this->enqueue($message);
90 }
91
92 /**
93 * Flushes our queue of messages by batching them to the server
94 */
95 public function flush()
96 {
97 $count = count($this->queue);
98 $success = true;
99
100 while ($count > 0 && $success) {
101 $batch = array_splice($this->queue, 0, min($this->batch_size, $count));
102 $success = $this->flushBatch($batch);
103
104 $count = count($this->queue);
105 }
106
107 return $success;
108 }
109
110 /**
111 * Adds an item to our queue.
112 * @param mixed $item
113 * @return boolean whether call has succeeded
114 */
115 public function enqueue($item)
116 {
117 $count = count($this->queue);
118
119 if ($count > $this->max_queue_size) {
120 return false;
121 }
122
123 $count = array_push($this->queue, $item);
124
125 if ($count >= $this->batch_size) {
126 return $this->flush(); // return ->flush() result: true on success
127 }
128
129 return true;
130 }
131
132 /**
133 * Given a batch of messages the method returns
134 * a valid payload.
135 *
136 * @param {Array} $batch
137 * @return {Array}
138 */
139 protected function payload($batch)
140 {
141 return array(
142 "batch" => $batch,
143 "api_key" => $this->apiKey,
144 );
145 }
146 }
147