Skip to content

Commit

Permalink
implement create() template
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Feb 28, 2019
1 parent 9a7cb58 commit 0ff27d2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/Repec.php
Expand Up @@ -662,32 +662,14 @@ private function createPaperTemplate(ContentEntityInterface $entity) {
/** @var array $template */
$template = $this->getEntityTemplate($entity);

$serieDirectoryConfig = $this->getEntityBundleSettings('serie_directory', $entity->getEntityTypeId(), $entity->bundle());
$directory = $this->getArchiveDirectory() . $serieDirectoryConfig . '/';

if (!empty($directory) &&
file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {

$fileName = $serieDirectoryConfig . '_' . $entity->getEntityTypeId() . '_' . $entity->id() . '.rdf';

$content = '';
foreach ($template as $item) {
if (!empty($item['value'])) {
$content .= $item['attribute'] . ': ' . $item['value'] . "\n";
}
}

if (!file_put_contents($directory . '/' . $fileName, $content)) {
$this->messenger->addError(t('File @file_name could not be created', [
'@file_name' => $fileName,
]));
}
/** @var \Drupal\repec\Series\Base $template_class */
$template_class = $this->templateFactory->create($this->getEntityBundleSettings('serie_type', $entity->getEntityTypeId(), $entity->bundle()), $entity);

try {
$template_class->create($template);
}
else {
$this->messenger->addError(t('Directory @path could not be created.', [
'@path' => $directory,
]));
catch (\Exception $e) {
$this->messenger->addError($e->getMessage());
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/Series/Base.php
Expand Up @@ -70,4 +70,50 @@ public function __construct(ImmutableConfig $settings, EntityTypeManagerInterfac
*/
abstract public function getDefault() : array;

/**
* Creates the template.
*
* @param array $template
* The template structure.
*
* @throws \Drupal\repec\Series\CreateException
*/
public function create(array $template) {
/** @var array $bundle_settings */
// The following is already done in
// \Drupal\repec\Repec::getEntityBundleSettings
// TODO: Move this to a parent service or something.
$bundle_settings = unserialize($this->settings->get("repec_bundle.{$this->entity->getEntityTypeId()}.{$this->entity->bundle()}"));

/** @var string $serie_directory_config */
$serie_directory_config = $bundle_settings['serie_directory'];

// The following is already done in \Drupal\repec\Repec::getArchiveDirectory
// TODO: Move this to a parent service or something.
$archive_directory = "public://{$this->settings->get('base_path')}/{$this->settings->get('archive_code')}/";

$directory = "{$archive_directory}{$serie_directory_config}/";

if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
throw new CreateException($this->t('Directory @path could not be created.', [
'@path' => $directory,
]));
}

$file_name = "{$serie_directory_config}_{$this->entity->getEntityTypeId()}_{$this->entity->id()}.rdf";

$content = '';
foreach ($template as $item) {
if (!empty($item['value'])) {
$content .= $item['attribute'] . ': ' . $item['value'] . "\n";
}
}

if (!file_put_contents("$directory/$file_name", $content)) {
throw new CreateException($this->t('File @file_name could not be created.', [
'@file_name' => $file_name,
]));
}
}

}
10 changes: 10 additions & 0 deletions src/Series/CreateException.php
@@ -0,0 +1,10 @@
<?php

namespace Drupal\repec\Series;

/**
* Class CreateException.
*
* Custom exception handler for template creation.
*/
class CreateException extends \Exception {}

0 comments on commit 0ff27d2

Please sign in to comment.