| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Consumes messages and writes them to a file |
| 5 |
*/ |
| 6 |
class Imagify_WPMedia_ConsumerStrategies_FileConsumer extends Imagify_WPMedia_ConsumerStrategies_AbstractConsumer { |
| 7 |
|
| 8 |
/** |
| 9 |
* @var string path to a file that we want to write the messages to |
| 10 |
*/ |
| 11 |
private $_file; |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* Creates a new FileConsumer and assigns properties from the $options array |
| 16 |
* @param array $options |
| 17 |
*/ |
| 18 |
function __construct($options) { |
| 19 |
parent::__construct($options); |
| 20 |
|
| 21 |
// what file to write to? |
| 22 |
$this->_file = isset($options['file']) ? $options['file'] : dirname(__FILE__)."/../../messages.txt"; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Append $batch to a file |
| 28 |
* @param array $batch |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
public function persist($batch) { |
| 32 |
if (count($batch) > 0) { |
| 33 |
return file_put_contents($this->_file, json_encode($batch)."\n", FILE_APPEND | LOCK_EX) !== false; |
| 34 |
} else { |
| 35 |
return true; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|