From fa82048c64c36f73261b2b9f277ed1bf3d1efd26 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 22 Feb 2013 14:22:37 -0500 Subject: [PATCH] Pass date converter to assignMetadata rather than creating a new object every time. --- module/VuFind/config/module.config.php | 8 +++++++- .../src/VuFind/Controller/UpgradeController.php | 3 ++- module/VuFind/src/VuFind/Db/Row/Resource.php | 6 +++--- module/VuFind/src/VuFind/Db/Table/Resource.php | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index c90ac39d9d5..a4fb642671b 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -300,10 +300,16 @@ $config = array( ), 'db_table' => array( 'abstract_factories' => array('VuFind\Db\Table\PluginFactory'), + 'factories' => array( + 'resource' => function ($sm) { + return new \VuFind\Db\Table\Resource( + $sm->getServiceLocator()->get('VuFind\DateConverter') + ); + }, + ), 'invokables' => array( 'changetracker' => 'VuFind\Db\Table\ChangeTracker', 'comments' => 'VuFind\Db\Table\Comments', - 'resource' => 'VuFind\Db\Table\Resource', 'resourcetags' => 'VuFind\Db\Table\ResourceTags', 'search' => 'VuFind\Db\Table\Search', 'session' => 'VuFind\Db\Table\Session', diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php index 053dd5cad6b..b959bb02ed2 100644 --- a/module/VuFind/src/VuFind/Controller/UpgradeController.php +++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php @@ -470,11 +470,12 @@ class UpgradeController extends AbstractBase // Process submit button: if (strlen($this->params()->fromPost('submit', '')) > 0) { + $converter = $this->getServiceLocator()->get('VuFind\DateConverter'); foreach ($problems as $problem) { try { $driver = $this->getRecordLoader() ->load($problem->record_id, $problem->source); - $problem->assignMetadata($driver)->save(); + $problem->assignMetadata($driver, $converter)->save(); } catch (RecordMissingException $e) { $this->session->warnings->append( "Unable to load metadata for record " diff --git a/module/VuFind/src/VuFind/Db/Row/Resource.php b/module/VuFind/src/VuFind/Db/Row/Resource.php index 111fbd9ceca..ef708085ba5 100644 --- a/module/VuFind/src/VuFind/Db/Row/Resource.php +++ b/module/VuFind/src/VuFind/Db/Row/Resource.php @@ -121,11 +121,12 @@ class Resource extends ServiceLocatorAwareGateway * Use a record driver to assign metadata to the current row. Return the * current object to allow fluent interface. * - * @param \VuFind\RecordDriver\AbstractBase $driver The record driver. + * @param \VuFind\RecordDriver\AbstractBase $driver The record driver + * @param \VuFind\Date\Converter $converter Date converter * * @return \VuFind\Db\Row\Resource */ - public function assignMetadata($driver) + public function assignMetadata($driver, \VuFind\Date\Converter $converter) { // Grab title -- we have to have something in this field! $this->title = $driver->tryMethod('getSortTitle'); @@ -142,7 +143,6 @@ class Resource extends ServiceLocatorAwareGateway // Try to find a year; if not available, just leave the default null: $dates = $driver->tryMethod('getPublicationDates'); if (isset($dates[0]) && strlen($dates[0]) > 4) { - $converter = new DateConverter(); try { $year = $converter->convertFromDisplayDate('Y', $dates[0]); } catch (DateException $e) { diff --git a/module/VuFind/src/VuFind/Db/Table/Resource.php b/module/VuFind/src/VuFind/Db/Table/Resource.php index aca1f03a9c7..cb48373a95b 100644 --- a/module/VuFind/src/VuFind/Db/Table/Resource.php +++ b/module/VuFind/src/VuFind/Db/Table/Resource.php @@ -39,11 +39,21 @@ use Zend\Db\Sql\Expression; */ class Resource extends Gateway { + /** + * Date converter + * + * @var \VuFind\Date\Converter + */ + protected $dateConverter; + /** * Constructor + * + * @param \VuFind\Date\Converter $converter Date converter */ - public function __construct() + public function __construct(\VuFind\Date\Converter $converter) { + $this->dateConverter = $converter; parent::__construct('resource', 'VuFind\Db\Row\Resource'); } @@ -84,7 +94,7 @@ class Resource extends Gateway } // Load metadata into the database for sorting/failback purposes: - $result->assignMetadata($driver); + $result->assignMetadata($driver, $this->dateConverter); // Save the new row. $result->save(); -- GitLab