/**
  * Persist template and move zip archive's elements.
  *
  * @param Layout $layout
  */
 public function save(Layout $layout)
 {
     $archiveDir = $this->getUploadDir() . '/archives';
     $templateDir = $this->getUploadDir() . '/templates';
     $tmpDir = $this->getUploadDir() . '/tmp/' . time();
     // Copy the archive to the archive directory
     $file = $layout->getFile()->move($archiveDir, $layout->getFile()->getClientOriginalName());
     // Extract the archive
     $zip = new \ZipArchive();
     $zip->open($archiveDir . '/' . $layout->getFile()->getClientOriginalName());
     $zip->extractTo($tmpDir);
     $zip->close();
     $id = $this->getUniqueId($layout);
     $config = $this->readConfiguration($tmpDir);
     // Move the assets
     $this->moveFiles(array('*.png', '*.jpg'), $tmpDir, $templateDir . '/img/' . $id);
     $this->moveFiles('*.twig', $tmpDir, $templateDir . '/twig/' . $id);
     $this->moveFiles('*.css', $tmpDir, $templateDir . '/css/' . $id, false);
     // If the layout has a fonts directory, we copy this directory to the css one.
     if ($fontsDirs = $this->getDirectories($tmpDir, 'fonts')) {
         $this->filesystem->remove($templateDir . '/css/' . $id . '/fonts');
         $this->filesystem->rename(current($fontsDirs), $templateDir . '/css/' . $id . '/fonts', true);
     }
     // Remove the tmp directory
     $this->filesystem->remove($tmpDir);
     $this->saveInDb($config['label'], 'uploads/' . $id . '/' . $config['templateName'], '/bundles/canaltpmtt/img/uploads/' . $id . '/' . $config['previewFileName'], $config['orientation']);
 }