Files
GestioneSpiaggia.it/library/App/Model/Mongo/Adapter.php
T
2021-09-17 12:41:45 +02:00

76 lines
1.9 KiB
PHP

<?php
class App_Model_Mongo_Adapter
{
const MONGO_DB_ADAPTER_REG_NAME = 'MONGO_DB_ADAPTER_REG_NAME';
const MONGO_DB_NAME = 'MONGO_DB_NAME';
protected $_username = null;
protected $_password = null;
protected $_host = '127.0.0.1';
protected $_port = '27017';
protected $_connectionUri = null;
protected $_dbname = null;
protected $_mongoClient;
public function __construct(array $connectionData = array())
{
//Default Mongo Uri
$this->_connectionUri = "mongodb://$this->_host:$this->_port";
//Autenticazione mongoDB
if ($connectionData['user'] && ['password']) {
$this->_username = $connectionData['user'];
$this->_password = $connectionData['password'];
$this->_host = ($connectionData['host']) ? $connectionData['host'] : $this->_host;
$this->_port = ($connectionData['port']) ? $connectionData['port'] : $this->_port;
$mongoAuth = "$this->_username:$this->_password";
$mongoHost = "$this->_host:$this->_port";
//AuthMongoUri
$this->_connectionUri = "mongodb://$mongoAuth@$mongoHost";
}
if($connectionData['dbname']){
$this->_dbname = $connectionData['dbname'];
}
}
/**
* @return \MongoDB\Client
*/
public function getMongoClient(): \MongoDB\Client
{
if(!$this->_mongoClient){
$this->_mongoClient = new MongoDB\Client($this->_connectionUri);
}
return $this->_mongoClient;
}
/**
* @return string|null
*/
public function getDbname(): string
{
return $this->_dbname;
}
/**
* @param string $dbname
*/
public function setDbname(string $dbname): void
{
$this->_dbname = $dbname;
Zend_Registry::set(self::MONGO_DB_ADAPTER_REG_NAME,$this);
}
}