Select Git revision
jest-puppeteer.config.js
ProjectController.php 8.02 KiB
<?php
namespace App\Controller;
use App\Entity\Capsule;
use App\Exception\ZipArchiveNotOpeningException;
use App\Form\EditVideoUrlFormType;
use App\Repository\CapsuleRepository;
use App\Retriever\ProjectRetriever;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Config\Util\Exception\XmlParsingException;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use ZipArchive;
class ProjectController extends AbstractController
{
public function __construct(private TranslatorInterface $translator, private Filesystem $file_system)
{
}
#[Route('/{_locale<%app.supported_locales%>}/project/create', name:'create_project', methods:['POST'])]
public function create(
Capsule $capsule,
string $video_url
): Response {
chdir('../legacy/');
$capsule_name = $capsule->getName();
$capsule_directory = $capsule->getLinkPath();
if (file_exists($capsule_directory)) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.already_exists',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
$this->createDefaultProjectStructure($capsule_directory);
$this->configureXMLProjectFile($capsule_directory, $video_url, $capsule);
$this->createOrUpdatePasswordFile($capsule_directory, $capsule->getPassword());
$this->addFlash(
'success',
$this->translator->trans(
'capsule.created_success',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
#[Route('/{_locale<%app.supported_locales%>}/project/duplicate', name:'duplicate_project', methods:['POST'])]
public function duplicate(
Capsule $capsule,
string $parent_directory,
Filesystem $file_system
): Response {
chdir('../legacy/');
$capsule_name = $capsule->getName();
$capsule_directory = $capsule->getLinkPath();
if (file_exists($capsule_directory)) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.already_exists',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
$this->createDefaultProjectStructure($capsule_directory);
$this->replaceTheWholeFileDirectoryWithParentOne($file_system, $parent_directory, $capsule_directory);
$this->createOrUpdatePasswordFile($capsule_directory, $capsule->getPassword());
$this->addFlash(
'success',
$this->translator->trans(
'capsule.duplicate.success',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
#[Route('/{_locale<%app.supported_locales%>}/capsule/{capsule_id}/edit_video_url', name:'edit_video_url')]
public function editVideoUrl(
int $capsule_id,
Request $request,
CapsuleRepository $capsule_repository
): Response {
$capsule = $capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception('The retrieved capsule is not an instance of Capsule.');
}
$file_system = new Filesystem();
$parent_directory_name = $capsule->getLinkPath();
$parent_directory_exists = $file_system->exists('../legacy/' . $parent_directory_name);
if (! $parent_directory_exists) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.not_exist',
[
'capsule_name' => $capsule->getName()
]
)
);
return $this->redirectToRoute('capsule_list');
}
$project_retriever = new ProjectRetriever($capsule);
$current_video_url = $project_retriever->getVideoUrl();
$form = $this->createForm(EditVideoUrlFormType::class, ['current_video_url' => $current_video_url]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$new_video_url = $form->get('new_video_url')->getData();
$project_directory = $capsule->getLinkPath();
chdir('../legacy/');
$this->configureXMLProjectFile($project_directory, $new_video_url, $capsule);
$this->addFlash(
'success',
$this->translator->trans(
'capsule.edit.video_url.success'
)
);
$url_redirect = $request->query->get('returnUrl');
if ($url_redirect) {
return $this->redirect($url_redirect);
}
return $this->redirectToRoute('capsule_list', [
'capsule_id' => $capsule_id
]);
}
return $this->renderForm('project/edit_video_url.html.twig', [
'editVideoUrlForm' => $form
]);
}
private function createDefaultProjectStructure(string $capsule_directory): void
{
$prototype_directory_root = '../capsule-prototype';
$this->file_system->mirror(
Path::join($prototype_directory_root),
Path::join($capsule_directory),
null,
['override' => true, 'delete' => true]
);
// Remove unnecessary files and directories
$this->file_system->remove(
[
Path::join($capsule_directory, 'js'),
Path::join($capsule_directory, 'css'),
Path::join($capsule_directory, 'webfonts'),
Path::join($capsule_directory, 'README.md')
]
);
}
private function configureXMLProjectFile(
string $project_directory,
string $video_url,
Capsule $capsule
): void {
$project_xml_file = $project_directory . "/file/project.xml";
$xml_file_content = simplexml_load_file($project_xml_file);
if (false === $xml_file_content) {
throw new FileNotFoundException("The XML project file could not be found");
}
/** @phpstan-ignore-next-line */
$xml_file_content->video['url'] = $video_url;
$xml_file_content->addChild('projectMeta');
/** @phpstan-ignore-next-line */
$xml_file_content->projectMeta['ctg'] = 'Title';
/** @phpstan-ignore-next-line */
$xml_file_content->projectMeta['cnt'] = $capsule->getName();
$video_url_XML_tag_is_filled = $xml_file_content->saveXML($project_xml_file);
if (false === $video_url_XML_tag_is_filled) {
throw new XmlParsingException('Video URL could not be written in XML project file');
}
}
private function createOrUpdatePasswordFile(string $capsule_directory, string $password): void
{
$project_password_file = $capsule_directory . '/file/projectPassword.txt';
file_put_contents($project_password_file, $password);
}
private function replaceTheWholeFileDirectoryWithParentOne(
FileSystem $file_system,
string $parent_directory,
string $child_directory
): void {
$file_directory_path = '/file/';
$file_system->mirror(
$parent_directory . $file_directory_path,
$child_directory . $file_directory_path,
null,
['override' => true]
);
}
}