imagify
/
vendor
/
wp-media
/
wp-mixpanel
/
src
/
Classes
/
ConsumerStrategies
/
AbstractConsumer.php
AbstractConsumer.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at vendor/wp-media/wp-mixpanel/src/Classes/ConsumerStrategies/AbstractConsumer.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Provides some base methods for use by a Consumer implementation |
| 5 | */ |
| 6 | abstract class Imagify_WPMedia_ConsumerStrategies_AbstractConsumer extends Imagify_WPMedia_Base_MixpanelBase { |
| 7 | |
| 8 | /** |
| 9 | * Creates a new AbstractConsumer |
| 10 | * @param array $options |
| 11 | */ |
| 12 | function __construct($options = array()) { |
| 13 | |
| 14 | parent::__construct($options); |
| 15 | |
| 16 | if ($this->_debug()) { |
| 17 | $this->_log("Instantiated new Consumer"); |
| 18 | } |
| 19 | |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Encode an array to be persisted |
| 24 | * @param array $params |
| 25 | * @return string |
| 26 | */ |
| 27 | protected function _encode($params) { |
| 28 | return base64_encode(json_encode($params)); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Handles errors that occur in a consumer |
| 33 | * @param $code |
| 34 | * @param $msg |
| 35 | */ |
| 36 | protected function _handleError($code, $msg) { |
| 37 | if (isset($this->_options['error_callback'])) { |
| 38 | $handler = $this->_options['error_callback']; |
| 39 | call_user_func($handler, $code, $msg); |
| 40 | } |
| 41 | |
| 42 | if ($this->_debug()) { |
| 43 | $arr = debug_backtrace(); |
| 44 | $class = get_class($arr[0]['object']); |
| 45 | $line = $arr[0]['line']; |
| 46 | error_log ( "[ $class - line $line ] : " . print_r($msg, true) ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Number of requests/batches that will be processed in parallel. |
| 52 | * @return int |
| 53 | */ |
| 54 | public function getNumThreads() { |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Persist a batch of messages in whatever way the implementer sees fit |
| 60 | * @param array $batch an array of messages to consume |
| 61 | * @return boolean success or fail |
| 62 | */ |
| 63 | abstract function persist($batch); |
| 64 | } |