Source of file MongoPayloadStoreHandler.php
Size: 1,848 Bytes - Last Modified: 2021-12-23T10:01:46+00:00
/var/www/docs.ssmods.com/process/src/code/handler/MongoPayloadStoreHandler.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | <?php use MongoDB\Client; class MongoPayloadStoreHandler extends PayloadStoreHandler { private static $host = '127.0.0.1'; private static $port = 27017; /** * @var Client */ private $client; /** * @var string */ private $db; /** * @var string */ private $collection; /** * MongoPayloadStoreHandler constructor. * * @param array $config */ public function __construct($config) { $host = defined('SS_MONGO_HOST') ? SS_MONGO_HOST : Config::inst()->get(MongoPayloadStoreHandler::class, 'host'); $port = defined('SS_MONGO_PORT') ? SS_MONGO_PORT : Config::inst()->get(MongoPayloadStoreHandler::class, 'port'); $this->client = new Client("mongodb://$host:$port"); $this->db = $config['db']; $this->collection = $config['collection']; } public function read(string $key): array { $document = $this->getCollection()->findOne([ 'key' => $key ]); return $document ? (array)$document : []; } public function write(string $key, array $payload) { $this->getCollection()->updateOne( ['key' => $key], ['$set' => [ 'payload' => $payload ]], ['upsert' => true] ); } public function delete(string $key) { $this->getCollection()->deleteOne([ 'key' => $key ]); } public function info(string $option = null): array { return $this->client->getTypeMap(); } public function getName(): string { return 'mongodb'; } /** * @return \MongoDB\Collection */ private function getCollection() { return $this->client->{$this->db}->{$this->collection}; } } |