From 517e4ca5435106ab5304849248cbea4e9dffd4b0 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 19 Apr 2014 13:20:54 +0200 Subject: split up api class for easier testing and clearer code --- db/feedmapper.php | 6 +++--- db/foldermapper.php | 6 +++--- db/itemmapper.php | 6 +++--- db/mapper.php | 13 +++++++------ db/mapperfactory.php | 16 +++++++++------- db/postgres/itemmapper.php | 6 +++--- 6 files changed, 28 insertions(+), 25 deletions(-) (limited to 'db') diff --git a/db/feedmapper.php b/db/feedmapper.php index 4d88a4667..06267b19d 100644 --- a/db/feedmapper.php +++ b/db/feedmapper.php @@ -25,14 +25,14 @@ namespace OCA\News\Db; -use \OCA\News\Core\API; +use \OCA\News\Core\Db; class FeedMapper extends Mapper implements IMapper { - public function __construct(API $api) { - parent::__construct($api, 'news_feeds'); + public function __construct(Db $db) { + parent::__construct($db, 'news_feeds', '\OCA\News\Db\Feed'); } diff --git a/db/foldermapper.php b/db/foldermapper.php index d273ce159..f3bedd70e 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -25,13 +25,13 @@ namespace OCA\News\Db; -use \OCA\News\Core\API; +use \OCA\News\Core\Db; class FolderMapper extends Mapper implements IMapper { - public function __construct(API $api) { - parent::__construct($api, 'news_folders'); + public function __construct(Db $db) { + parent::__construct($db, 'news_folders', '\OCA\News\Db\Folder'); } public function find($id, $userId){ diff --git a/db/itemmapper.php b/db/itemmapper.php index aea0180d7..33b09e357 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -24,12 +24,12 @@ namespace OCA\News\Db; -use \OCA\News\Core\API; +use \OCA\News\Core\Db; class ItemMapper extends Mapper implements IMapper { - public function __construct(API $api){ - parent::__construct($api, 'news_items', '\OCA\News\Db\Item'); + public function __construct(Db $db){ + parent::__construct($db, 'news_items', '\OCA\News\Db\Item'); } diff --git a/db/mapper.php b/db/mapper.php index 42f1efa93..dae59a84e 100644 --- a/db/mapper.php +++ b/db/mapper.php @@ -26,7 +26,7 @@ namespace OCA\News\Db; -use \OCA\News\Core\API; +use \OCA\News\Core\Db; /** @@ -37,15 +37,16 @@ abstract class Mapper { protected $tableName; protected $entityClass; + private $db; /** - * @param API $api Instance of the API abstraction layer + * @param Db $db Instance of the Db abstraction layer * @param string $tableName the name of the table. set this to allow entity * @param string $entityClass the name of the entity that the sql should be * mapped to queries without using sql */ - public function __construct(API $api, $tableName, $entityClass=null){ - $this->api = $api; + public function __construct(Db $db, $tableName, $entityClass=null){ + $this->db = $db; $this->tableName = '*PREFIX*' . $tableName; // if not given set the entity name to the class without the mapper part @@ -114,7 +115,7 @@ abstract class Mapper { $this->execute($sql, $params); - $entity->setId((int) $this->api->getInsertId($this->tableName)); + $entity->setId((int) $this->db->getInsertId($this->tableName)); return $entity; } @@ -177,7 +178,7 @@ abstract class Mapper { * @return \PDOStatement the database query result */ protected function execute($sql, array $params=array(), $limit=null, $offset=null){ - $query = $this->api->prepareQuery($sql, $limit, $offset); + $query = $this->db->prepareQuery($sql, $limit, $offset); $index = 1; // bindParam is 1 indexed foreach($params as $param) { diff --git a/db/mapperfactory.php b/db/mapperfactory.php index 564819356..178e62160 100644 --- a/db/mapperfactory.php +++ b/db/mapperfactory.php @@ -25,25 +25,27 @@ namespace OCA\News\Db; -use \OCA\News\Core\API; +use \OCA\News\Core\Settings; +use \OCA\News\Core\Db; class MapperFactory { - private $api; + private $settings; - public function __construct(API $api) { - $this->api = $api; + public function __construct(Settings $settings, Db $db) { + $this->settings = $settings; + $this->db = $db; } public function getItemMapper() { - switch($this->api->getSystemValue('dbtype')) { + switch($this->settings->getSystemValue('dbtype')) { case 'pgsql': - return new \OCA\News\Db\Postgres\ItemMapper($this->api); + return new \OCA\News\Db\Postgres\ItemMapper($this->db); break; default: - return new ItemMapper($this->api); + return new ItemMapper($this->db); break; } } diff --git a/db/postgres/itemmapper.php b/db/postgres/itemmapper.php index e24ea373d..d5395f008 100644 --- a/db/postgres/itemmapper.php +++ b/db/postgres/itemmapper.php @@ -24,7 +24,7 @@ namespace OCA\News\Db\Postgres; -use \OCA\News\Core\API; +use \OCA\News\Core\Db; use \OCA\News\Db\DoesNotExistException; use \OCA\News\Db\MultipleObjectsReturnedException; use \OCA\News\Db\Mapper; @@ -33,8 +33,8 @@ use \OCA\News\Db\StatusFlag; class ItemMapper extends \OCA\News\Db\ItemMapper { - public function __construct(API $api){ - parent::__construct($api); + public function __construct(Db $db){ + parent::__construct($db, 'news_items', '\OCA\News\Db\Item'); } -- cgit v1.2.3