Skip to content

Commit

Permalink
Merge pull request #4 from a-kovrigin/8.x-1.x
Browse files Browse the repository at this point in the history
Create temporary file only when generating the writer output.
  • Loading branch information
Niklan committed Nov 12, 2021
2 parents 06a7786 + 0c79ff3 commit a789ac2
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/YandexYmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
class YandexYmlGenerator implements YandexYmlGeneratorInterface {

protected $counter;

/**
* The XML writer.
*
Expand Down Expand Up @@ -77,14 +75,6 @@ public function __construct(
$this->dateTime = $date_time;
$this->dateFormatter = $date_formatter;
$this->fileSystem = $file_system;

// Prepare temporary file.
$this->tempFilePath = $this->fileSystem->tempnam('temporary://yandex_yml', 'yml_');
// Initialization of file.
$this->writer = new XMLWriter();
$this->writer->openURI($this->tempFilePath);
$this->writer->setIndentString("\t");
$this->writer->setIndent(TRUE);
}

/**
Expand All @@ -94,14 +84,38 @@ public function __construct(
* leading dashes for path.
*/
public function generateFile($filename = 'products.xml', $destination_path = 'public://') {
$this->buildData();
$this->prepareTemporaryFile()
->setWriter()
->buildData();

$this->fileSystem->copy(
$this->tempFilePath,
$destination_path . $filename,
FileSystemInterface::EXISTS_REPLACE
);
}

/**
* Prepare temporary file.
*/
protected function prepareTemporaryFile() {
$this->tempFilePath = $this->fileSystem->tempnam('temporary://yandex_yml', 'yml_');

return $this;
}

/**
* Initialize the XML writer.
*/
protected function setWriter() {
$this->writer = new XMLWriter();
$this->writer->openURI($this->tempFilePath);
$this->writer->setIndentString("\t");
$this->writer->setIndent(TRUE);

return $this;
}

/**
* Write elements to writer object.
*/
Expand Down

0 comments on commit a789ac2

Please sign in to comment.