Added Config; concealed MongoDB dependency

This commit is contained in:
Marcin Kurczewski 2014-08-30 12:07:34 +02:00
parent 1e3e3f94d8
commit 011d803bd0
7 changed files with 57 additions and 16 deletions

6
src/Config.php Normal file
View file

@ -0,0 +1,6 @@
<?php
namespace Szurubooru;
final class Config extends \ArrayObject
{
}

View file

@ -7,10 +7,10 @@ abstract class AbstractDao implements ICrudDao
protected $collection; protected $collection;
protected $entityName; protected $entityName;
public function __construct(\MongoDB $mongoDb, $collectionName, $entityName) public function __construct(\Szurubooru\Config $config, $collectionName, $entityName)
{ {
$this->db = $mongoDb; $this->db = (new \Szurubooru\DatabaseConnection($config))->getDatabase();
$this->collection = $mongoDb->selectCollection($collectionName); $this->collection = $this->db->selectCollection($collectionName);
$this->entityName = $entityName; $this->entityName = $entityName;
} }

View file

@ -3,8 +3,8 @@ namespace Szurubooru\Dao;
final class PostDao extends AbstractDao implements ICrudDao final class PostDao extends AbstractDao implements ICrudDao
{ {
public function __construct(\MongoDB $mongoDb) public function __construct(\Szurubooru\Config $config)
{ {
parent::__construct($mongoDb, 'posts', '\Szurubooru\Entities\Post'); parent::__construct($config, 'posts', '\Szurubooru\Entities\Post');
} }
} }

View file

@ -0,0 +1,34 @@
<?php
namespace Szurubooru;
final class DatabaseConnection
{
private $database;
private $connection;
public function __construct(\Szurubooru\Config $config)
{
$connectionString = $this->getConnectionString($config);
$this->connection = new \MongoClient($connectionString);
$this->database = $this->connection->selectDb($config->databaseName);
}
public function getConnection()
{
return $this->connection;
}
public function getDatabase()
{
return $this->database;
}
private function getConnectionString(\Szurubooru\Config $config)
{
return sprintf(
'mongodb://%s:%d/%s',
$config->databaseHost,
$config->databasePort,
$config->databaseName);
}
}

View file

@ -5,9 +5,9 @@ final class UpgradeService
{ {
private $db; private $db;
public function __construct(\MongoDB $db) public function __construct(\Szurubooru\Config $config)
{ {
$this->db = $db; $this->db = (new DatabaseConnection($config))->getDatabase();
} }
public function prepareForUsage() public function prepareForUsage()

View file

@ -12,10 +12,11 @@ abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
$host = 'localhost'; $host = 'localhost';
$port = 27017; $port = 27017;
$database = 'test'; $database = 'test';
$connectingString = sprintf('mongodb://%s:%d/%s', $host, $port, $database); $this->config = new \Szurubooru\Config();
$this->connection = new \Mongo($connectingString); $this->config->databaseHost = 'localhost';
$this->db = $this->connection->selectDb($database); $this->config->databasePort = 27017;
$this->upgradeService = new \Szurubooru\UpgradeService($this->db); $this->config->databaseName = 'test';
$this->upgradeService = new \Szurubooru\UpgradeService($this->config);
$this->upgradeService->prepareForUsage(); $this->upgradeService->prepareForUsage();
} }

View file

@ -5,7 +5,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
{ {
public function testSaving() public function testSaving()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->db); $postDao = new \Szurubooru\Dao\PostDao($this->config);
$post = new \Szurubooru\Entities\Post(); $post = new \Szurubooru\Entities\Post();
$post->name = 'test2'; $post->name = 'test2';
@ -24,7 +24,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
public function testGettingAll() public function testGettingAll()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->db); $postDao = new \Szurubooru\Dao\PostDao($this->config);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->name = 'test2';
@ -45,7 +45,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
public function testGettingById() public function testGettingById()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->db); $postDao = new \Szurubooru\Dao\PostDao($this->config);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->name = 'test2';
@ -63,7 +63,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
public function testDeletingAll() public function testDeletingAll()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->db); $postDao = new \Szurubooru\Dao\PostDao($this->config);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->name = 'test2';
@ -84,7 +84,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
public function testDeletingById() public function testDeletingById()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->db); $postDao = new \Szurubooru\Dao\PostDao($this->config);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->name = 'test2';