tethys/app/Library/Xml/XmlModel.php

207 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2018-08-06 12:30:51 +00:00
<?php
/**
* Footer
*
* Main footer file for the theme.
*
* @category Components
* @package ResearchRepository
* @subpackage Publish
* @author Your Name <yourname@example.com>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://gisgba.geologie.ac.at
* @since 1.0.0
*/
namespace App\Library\Xml;
2018-09-10 13:09:10 +00:00
use App\Models\XmlCache;
2018-08-06 12:30:51 +00:00
use Illuminate\Support\Facades\Log;
use \Exception;
2018-08-06 12:30:51 +00:00
class XmlModel
{
/**
* Holds current configuration.
* @var Conf
*/
2018-09-10 13:09:10 +00:00
private $config = null;
2018-08-06 12:30:51 +00:00
/**
* Holds current xml strategy object.
* @var Strategy
*/
2018-09-10 13:09:10 +00:00
private $strategy = null;
2018-08-06 12:30:51 +00:00
/**
* TODO
* @var XmlCache
*/
2018-09-10 13:09:10 +00:00
private $cache = null;
2018-08-06 12:30:51 +00:00
/**
* Do some initial stuff like setting of a XML version and an empty
* configuration.
*/
public function __construct()
{
2018-09-10 13:09:10 +00:00
$this->strategy = new Strategy();// Opus_Model_Xml_Version1;
$this->config = new Conf();
$this->strategy->setup($this->config);
2018-08-06 12:30:51 +00:00
}
/**
* Set a new XML version with current configuration up.
*
* @param Strategy $strategy Version of Xml to process
*
* @return XmlModel fluent interface.
*/
public function setStrategy(Strategy $strategy)
{
2018-09-10 13:09:10 +00:00
$this->strategy = $strategy;
$this->strategy->setup($this->config);
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Set a new XML version with current configuration up.
*
* @param XmlCache $cache cach table
*
* @return XmlModel fluent interface.
*/
public function setXmlCache(XmlCache $cache)
{
2018-09-10 13:09:10 +00:00
$this->cache = $cache;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Return cache table.
*
* @return XmlCache
*/
public function getXmlCache()
{
2018-09-10 13:09:10 +00:00
return $this->cache;
2018-08-06 12:30:51 +00:00
}
/**
* Set the Model for XML generation.
*
2018-09-10 13:09:10 +00:00
* @param \App\Models\Dataset $model Model to serialize.
2018-08-06 12:30:51 +00:00
*
* @return XmlModel Fluent interface.
*/
public function setModel($model)
{
2018-09-10 13:09:10 +00:00
$this->config->model = $model;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* Define that empty fields (value===null) shall be excluded.
*
* @return XmlModel Fluent interface
*/
public function excludeEmptyFields()
{
2018-09-10 13:09:10 +00:00
$this->config->excludeEmpty = true;
2018-08-06 12:30:51 +00:00
return $this;
}
/**
* If a model has been set this method generates and returnes
* DOM representation of it.
*
* @return \DOMDocument DOM representation of the current Model.
*/
public function getDomDocument()
{
2018-09-10 13:09:10 +00:00
$dataset = $this->config->model;
2018-08-06 12:30:51 +00:00
$domDocument = $this->getDomDocumentFromXmlCache();
if (!is_null($domDocument)) {
return $domDocument;
}
2019-04-17 14:01:38 +00:00
//$domDocument == null -> create xml:
2018-09-10 13:09:10 +00:00
$domDocument = $this->strategy->getDomDocument();
2018-08-06 12:30:51 +00:00
//if caching is not desired, return domDocument
2018-09-10 13:09:10 +00:00
if (is_null($this->cache)) {
2018-08-06 12:30:51 +00:00
return $domDocument;
} else {
//create cache relation
2019-02-14 14:09:11 +00:00
// $this->cache->updateOrCreate(array(
// 'document_id' => $dataset->id,
// 'xml_version' => (int)$this->strategy->getVersion(),
// 'server_date_modified' => $dataset->server_date_modified,
// 'xml_data' => $domDocument->saveXML()
// ));
if (!$this->cache->document_id) {
$this->cache->document_id = $dataset->id;
}
$this->cache->xml_version = (int)$this->strategy->getVersion();
$this->cache->server_date_modified = $dataset->server_date_modified;
$this->cache->xml_data = $domDocument->saveXML();
2018-09-10 13:09:10 +00:00
$this->cache->save();
2018-08-06 12:30:51 +00:00
Log::debug(__METHOD__ . ' cache refreshed for ' . get_class($dataset) . '#' . $dataset->id);
return $domDocument;
}
}
/**
* This method tries to load the current model from the xml cache. Returns
* null in case of an error/cache miss/cache disabled. Returns DOMDocument
* otherwise.
*
* @return \DOMDocument DOM representation of the current Model.
*/
private function getDomDocumentFromXmlCache()
{
2018-09-10 13:09:10 +00:00
$dataset = $this->config->model;
if (null === $this->cache) {
2018-08-06 12:30:51 +00:00
//$logger->debug(__METHOD__ . ' skipping cache for ' . get_class($model));
Log::debug(__METHOD__ . ' skipping cache for ' . get_class($dataset));
return null;
}
2019-02-14 14:09:11 +00:00
$actuallyCached = $this->cache->hasValidEntry(
$dataset->id,
$dataset->server_date_modified
);
//no actual cache
if (true !== $actuallyCached) {
2018-08-06 12:30:51 +00:00
Log::debug(__METHOD__ . ' cache miss for ' . get_class($dataset) . '#' . $dataset->id);
return null;
2019-02-14 14:09:11 +00:00
}
//cache is actual return it for oai:
Log::debug(__METHOD__ . ' cache hit for ' . get_class($dataset) . '#' . $dataset->id);
try {
//return $this->_cache->get($model->getId(), (int) $this->_strategy->getVersion());
$cache = XmlCache::where('document_id', $dataset->id)->first();
2018-08-06 12:30:51 +00:00
return $cache->getDomDocument();
2019-02-14 14:09:11 +00:00
} catch (Exception $e) {
2019-03-18 13:32:29 +00:00
Log::warning(__METHOD__ . " Access to XML cache failed on " . get_class($dataset) .
'#' . $dataset->id . ". Trying to recover.");
2018-08-06 12:30:51 +00:00
}
2019-02-14 14:09:11 +00:00
return null;
// // $cache = XmlCache::where('document_id', $dataset->id)
// // ->first();// model or null
// if (!$cache) {
// Log::debug(__METHOD__ . ' cache miss for ' . get_class($dataset) . '#' . $dataset->id);
// return null;
// } else {
// return $cache->getDomDocument();
// }
2018-08-06 12:30:51 +00:00
}
}