Changed object initialization
DI FTW
This commit is contained in:
parent
6265a09d39
commit
f3096ffc5d
6 changed files with 61 additions and 44 deletions
|
@ -6,17 +6,22 @@ abstract class AbstractDao implements ICrudDao
|
|||
protected $db;
|
||||
protected $collection;
|
||||
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->entityName = $entityName;
|
||||
}
|
||||
|
||||
public function save(&$entity)
|
||||
{
|
||||
$arrayEntity = $this->makeArray($entity);
|
||||
$arrayEntity = $this->entityConverter->toArray($entity);
|
||||
if ($entity->id)
|
||||
{
|
||||
unset ($arrayEntity['_id']);
|
||||
|
@ -26,7 +31,7 @@ abstract class AbstractDao implements ICrudDao
|
|||
{
|
||||
$this->collection->insert($arrayEntity, ['safe' => true]);
|
||||
}
|
||||
$entity = $this->makeEntity($arrayEntity);
|
||||
$entity = $this->entityConverter->toEntity($arrayEntity);
|
||||
return $entity;
|
||||
}
|
||||
|
||||
|
@ -35,7 +40,7 @@ abstract class AbstractDao implements ICrudDao
|
|||
$entities = [];
|
||||
foreach ($this->collection->find() as $key => $arrayEntity)
|
||||
{
|
||||
$entity = $this->makeEntity($arrayEntity);
|
||||
$entity = $this->entityConverter->toEntity($arrayEntity);
|
||||
$entities[$key] = $entity;
|
||||
}
|
||||
return $entities;
|
||||
|
@ -44,7 +49,7 @@ abstract class AbstractDao implements ICrudDao
|
|||
public function getById($postId)
|
||||
{
|
||||
$arrayEntity = $this->collection->findOne(['_id' => new \MongoId($postId)]);
|
||||
return $this->makeEntity($arrayEntity);
|
||||
return $this->entityConverter->toEntity($arrayEntity);
|
||||
}
|
||||
|
||||
public function deleteAll()
|
||||
|
@ -56,26 +61,4 @@ abstract class AbstractDao implements ICrudDao
|
|||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
|
34
src/Dao/EntityConverter.php
Normal file
34
src/Dao/EntityConverter.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -3,8 +3,8 @@ namespace Szurubooru\Dao;
|
|||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ final class UpgradeService
|
|||
{
|
||||
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()
|
||||
|
|
|
@ -3,8 +3,7 @@ namespace Szurubooru\Tests;
|
|||
|
||||
abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $db;
|
||||
protected $connection;
|
||||
protected $databaseConnection;
|
||||
protected $upgradeService;
|
||||
|
||||
public function setUp()
|
||||
|
@ -12,11 +11,12 @@ abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
|
|||
$host = 'localhost';
|
||||
$port = 27017;
|
||||
$database = 'test';
|
||||
$this->config = new \Szurubooru\Config();
|
||||
$this->config->databaseHost = 'localhost';
|
||||
$this->config->databasePort = 27017;
|
||||
$this->config->databaseName = 'test';
|
||||
$this->upgradeService = new \Szurubooru\UpgradeService($this->config);
|
||||
$config = new \Szurubooru\Config();
|
||||
$config->databaseHost = 'localhost';
|
||||
$config->databasePort = 27017;
|
||||
$config->databaseName = 'test';
|
||||
$this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
|
||||
$this->upgradeService = new \Szurubooru\UpgradeService($this->databaseConnection);
|
||||
$this->upgradeService->prepareForUsage();
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
|||
{
|
||||
public function testSaving()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$post = new \Szurubooru\Entities\Post();
|
||||
$post->name = 'test2';
|
||||
|
@ -24,7 +24,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
|||
|
||||
public function testGettingAll()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$post1 = new \Szurubooru\Entities\Post();
|
||||
$post1->name = 'test2';
|
||||
|
@ -45,7 +45,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
|||
|
||||
public function testGettingById()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$post1 = new \Szurubooru\Entities\Post();
|
||||
$post1->name = 'test2';
|
||||
|
@ -63,7 +63,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
|||
|
||||
public function testDeletingAll()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$post1 = new \Szurubooru\Entities\Post();
|
||||
$post1->name = 'test2';
|
||||
|
@ -84,7 +84,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
|||
|
||||
public function testDeletingById()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$post1 = new \Szurubooru\Entities\Post();
|
||||
$post1->name = 'test2';
|
||||
|
|
Loading…
Reference in a new issue