<commit_before><?php

namespace Ddeboer\DocumentManipulationBundle\Manipulator;

use Zend\Service\LiveDocx\MailMerge;
use Symfony\Component\HttpFoundation\File\File;

class DocManipulator
{
    /**
     * @var MailMerge
     */
    private $liveDocx;

    public function getLiveDocx()
    {
        return $this->liveDocx;
    }

    public function setLiveDocx(MailMerge $liveDocx)
    {
        $this->liveDocx = $liveDocx;
    }

    public function merge(File $file, \Traversable $data, $outputFile, $format = 'pdf')
    {
        // Upload local template to server if it hasn't been uploaded yet
        if (!$this->liveDocx->templateExists($file->getFilename())) {
            $this->liveDocx->uploadTemplate($file->getPathname());
        }
        $this->liveDocx->setRemoteTemplate($file->getFilename());
//        $this->liveDocx->setLocalTemplate($file->getPathname());

        foreach ($data as $field => $value) {
            $this->liveDocx->assign($field, $value);
        }

        $this->liveDocx->createDocument();
        $document = $this->liveDocx->retrieveDocument($format);
        file_put_contents($outputFile, $document);

        return $outputFile;
    }
}<commit_msg>Use md5 hash instead of filename to determine if template must be uploaded<commit_after><?php

namespace Ddeboer\DocumentManipulationBundle\Manipulator;

use Zend\Service\LiveDocx\MailMerge;
use Symfony\Component\HttpFoundation\File\File;

class DocManipulator
{
    /**
     * @var MailMerge
     */
    private $liveDocx;

    public function getLiveDocx()
    {
        return $this->liveDocx;
    }

    public function setLiveDocx(MailMerge $liveDocx)
    {
        $this->liveDocx = $liveDocx;
    }

    public function merge(File $file, \Traversable $data, $outputFile, $format = 'pdf')
    {
        // Calculate MD5 hash for file
        $hash = md5_file($file->getPathname());
        $tmpFile = sys_get_temp_dir() . '/' . $hash;
        copy($file->getPathname(), $tmpFile);

        // Upload local template to server if it hasn't been uploaded yet
        if (!$this->liveDocx->templateExists($hash)) {
            $this->liveDocx->uploadTemplate($tmpFile);
        }

        $this->liveDocx->setLocalTemplate($file->getPathname());

        foreach ($data as $field => $value) {
            $this->liveDocx->assign($field, $value);
        }

        $this->liveDocx->createDocument();
        $document = $this->liveDocx->retrieveDocument($format);
        file_put_contents($outputFile, $document);

        return $outputFile;
    }
}
{'fraction_non_alphanumeric': 0.1371473354231975, 'fraction_numerical': 0.0011755485893416929, 'mean_word_length': 2.787833827893175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641684', 'n_tokens_mistral': 787, 'n_tokens_neox': 767, 'n_words': 149}