Changed object initialization

DI FTW
This commit is contained in:
Marcin Kurczewski 2014-08-30 17:10:45 +02:00
parent 6265a09d39
commit f3096ffc5d
6 changed files with 61 additions and 44 deletions

View file

@ -6,17 +6,22 @@ abstract class AbstractDao implements ICrudDao
protected $db; protected $db;
protected $collection; protected $collection;
protected $entityName; protected $entityName;
protected $entityConverter;
public function __construct(\Szurubooru\Config $config, $collectionName, $entityName) public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
$collectionName,
$entityName)
{ {
$this->db = (new \Szurubooru\DatabaseConnection($config))->getDatabase(); $this->entityConverter = new EntityConverter($entityName);
$this->db = $databaseConnection->getDatabase();
$this->collection = $this->db->selectCollection($collectionName); $this->collection = $this->db->selectCollection($collectionName);
$this->entityName = $entityName; $this->entityName = $entityName;
} }
public function save(&$entity) public function save(&$entity)
{ {
$arrayEntity = $this->makeArray($entity); $arrayEntity = $this->entityConverter->toArray($entity);
if ($entity->id) if ($entity->id)
{ {
unset ($arrayEntity['_id']); unset ($arrayEntity['_id']);
@ -26,7 +31,7 @@ abstract class AbstractDao implements ICrudDao
{ {
$this->collection->insert($arrayEntity, ['safe' => true]); $this->collection->insert($arrayEntity, ['safe' => true]);
} }
$entity = $this->makeEntity($arrayEntity); $entity = $this->entityConverter->toEntity($arrayEntity);
return $entity; return $entity;
} }
@ -35,7 +40,7 @@ abstract class AbstractDao implements ICrudDao
$entities = []; $entities = [];
foreach ($this->collection->find() as $key => $arrayEntity) foreach ($this->collection->find() as $key => $arrayEntity)
{ {
$entity = $this->makeEntity($arrayEntity); $entity = $this->entityConverter->toEntity($arrayEntity);
$entities[$key] = $entity; $entities[$key] = $entity;
} }
return $entities; return $entities;
@ -44,7 +49,7 @@ abstract class AbstractDao implements ICrudDao
public function getById($postId) public function getById($postId)
{ {
$arrayEntity = $this->collection->findOne(['_id' => new \MongoId($postId)]); $arrayEntity = $this->collection->findOne(['_id' => new \MongoId($postId)]);
return $this->makeEntity($arrayEntity); return $this->entityConverter->toEntity($arrayEntity);
} }
public function deleteAll() public function deleteAll()
@ -56,26 +61,4 @@ abstract class AbstractDao implements ICrudDao
{ {
$this->collection->remove(['_id' => new \MongoId($postId)]); $this->collection->remove(['_id' => new \MongoId($postId)]);
} }
private function makeArray($entity)
{
$arrayEntity = (array) $entity;
if (isset($entity->id))
{
$arrayEntity['_id'] = $arrayEntity['id'];
unset($arrayEntity['id']);
}
return $arrayEntity;
}
private function makeEntity($arrayEntity)
{
$entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName);
if (isset($entity->_id))
{
$entity->id = (string) $entity->_id;
unset($entity->_id);
}
return $entity;
}
} }

View file

@ -0,0 +1,34 @@
<?php
namespace Szurubooru\Dao;
final class EntityConverter
{
protected $entityName;
public function __construct($entityName)
{
$this->entityName = $entityName;
}
public function toArray($entity)
{
$arrayEntity = (array) $entity;
if (isset($entity->id))
{
$arrayEntity['_id'] = $arrayEntity['id'];
unset($arrayEntity['id']);
}
return $arrayEntity;
}
public function toEntity($arrayEntity)
{
$entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName);
if (isset($entity->_id))
{
$entity->id = (string) $entity->_id;
unset($entity->_id);
}
return $entity;
}
}

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(\Szurubooru\Config $config) public function __construct(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
parent::__construct($config, 'posts', '\Szurubooru\Entities\Post'); parent::__construct($databaseConnection, 'posts', '\Szurubooru\Entities\Post');
} }
} }

View file

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

View file

@ -3,8 +3,7 @@ namespace Szurubooru\Tests;
abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
{ {
protected $db; protected $databaseConnection;
protected $connection;
protected $upgradeService; protected $upgradeService;
public function setUp() public function setUp()
@ -12,11 +11,12 @@ abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
$host = 'localhost'; $host = 'localhost';
$port = 27017; $port = 27017;
$database = 'test'; $database = 'test';
$this->config = new \Szurubooru\Config(); $config = new \Szurubooru\Config();
$this->config->databaseHost = 'localhost'; $config->databaseHost = 'localhost';
$this->config->databasePort = 27017; $config->databasePort = 27017;
$this->config->databaseName = 'test'; $config->databaseName = 'test';
$this->upgradeService = new \Szurubooru\UpgradeService($this->config); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
$this->upgradeService = new \Szurubooru\UpgradeService($this->databaseConnection);
$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->config); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$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->config); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$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->config); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$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->config); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$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->config); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post1 = new \Szurubooru\Entities\Post(); $post1 = new \Szurubooru\Entities\Post();
$post1->name = 'test2'; $post1->name = 'test2';