From f3096ffc5daea8db8d20bcb0167697d5758c439d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Aug 2014 17:10:45 +0200 Subject: [PATCH] Changed object initialization DI FTW --- src/Dao/AbstractDao.php | 39 ++++++++++------------------------ src/Dao/EntityConverter.php | 34 +++++++++++++++++++++++++++++ src/Dao/PostDao.php | 4 ++-- src/UpgradeService.php | 4 ++-- tests/AbstractDatabaseTest.php | 14 ++++++------ tests/Dao/PostDaoTest.php | 10 ++++----- 6 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 src/Dao/EntityConverter.php diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index 21a40874..9811e2b2 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -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; - } } diff --git a/src/Dao/EntityConverter.php b/src/Dao/EntityConverter.php new file mode 100644 index 00000000..b7fed431 --- /dev/null +++ b/src/Dao/EntityConverter.php @@ -0,0 +1,34 @@ +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; + } +} diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index d64019a1..a61c899b 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -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'); } } diff --git a/src/UpgradeService.php b/src/UpgradeService.php index fa21e03b..c01dfa3e 100644 --- a/src/UpgradeService.php +++ b/src/UpgradeService.php @@ -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() diff --git a/tests/AbstractDatabaseTest.php b/tests/AbstractDatabaseTest.php index 4fad63d2..df36f1d9 100644 --- a/tests/AbstractDatabaseTest.php +++ b/tests/AbstractDatabaseTest.php @@ -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(); } diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index fb53a2e7..bd81a9e1 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -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';