webcron/src/Controller/JobController.php

87 lines
3.5 KiB
PHP
Raw Normal View History

2021-04-06 23:19:51 +02:00
<?php
2022-04-27 14:24:48 +02:00
namespace App\Controller;
2021-04-06 23:19:51 +02:00
2022-04-27 14:24:48 +02:00
use App\Entity\Job;
use App\Entity\Run;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2021-05-24 14:08:30 +02:00
use Symfony\Component\HttpFoundation\JsonResponse;
2021-04-06 23:19:51 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2022-04-27 14:24:48 +02:00
use Symfony\Component\HttpFoundation\Request;
2021-04-06 23:19:51 +02:00
use Symfony\Component\HttpFoundation\Response;
2022-04-27 14:24:48 +02:00
class JobController extends AbstractController
2021-04-06 23:19:51 +02:00
{
2022-04-27 14:24:48 +02:00
public function defaultAction(ManagerRegistry $doctrine): Response
2021-04-08 14:54:30 +02:00
{
2022-04-27 14:24:48 +02:00
$jobRepo = $doctrine->getRepository(Job::class);
2021-04-08 12:54:49 +02:00
$jobs = $jobRepo->getAllJobs();
return $this->render('job/index.html.twig', ['jobs' => $jobs]);
2021-04-06 23:19:51 +02:00
}
2021-04-08 14:54:30 +02:00
2022-04-27 14:24:48 +02:00
public function jobAction(Request $request, ManagerRegistry $doctrine, $id, $all = false): Response
2021-04-08 14:54:30 +02:00
{
2022-04-27 14:24:48 +02:00
$jobRepo = $doctrine->getRepository(Job::class);
$runRepo = $doctrine->getRepository(Run::class);
2021-05-24 14:08:30 +02:00
2022-04-27 14:24:48 +02:00
if($request->getMethod() == 'GET') {
2021-05-24 14:08:30 +02:00
$job = $jobRepo->getJob($id);
$runs = $runRepo->getRunsForJob($id, $all != 'all');
2021-05-27 11:46:30 +02:00
return $this->render('job/view.html.twig', ['job' => $job, 'runs' => $runs, 'allruns' => $all == 'all']);
2022-04-27 14:24:48 +02:00
} elseif($request->getMethod() == 'DELETE') {
2021-05-24 14:08:30 +02:00
$success = $jobRepo->deleteJob($id);
$this->addFlash('success', $success['message']);
2022-04-27 14:24:48 +02:00
return new JsonResponse(['return_path' => $this->GenerateUrl('job_index')]);
2021-05-24 14:08:30 +02:00
}
2021-04-08 14:54:30 +02:00
}
2021-04-12 12:23:50 +02:00
2022-04-27 14:24:48 +02:00
public function editAction(Request $request, ManagerRegistry $doctrine, $id)
2021-05-21 13:09:48 +02:00
{
2022-04-27 14:24:48 +02:00
if($request->getMethod() == 'GET') {
$jobRepo = $doctrine->getRepository(Job::class);
2021-05-21 13:09:48 +02:00
$job = $jobRepo->getJob($id, true);
return $this->render('job/edit.html.twig', $job);
2022-04-27 14:24:48 +02:00
} elseif($request->getMethod() == 'POST') {
$allValues = $request->request->all();
$jobRepo = $doctrine->getRepository(Job::class);
2021-05-21 13:09:48 +02:00
try {
$joboutput = $jobRepo->editJob($id, $allValues);
} catch (\InvalidArgumentException $e) {
$this->addFlash('danger', $e->getMessage());
2022-04-27 14:24:48 +02:00
return new RedirectResponse($this->GenerateUrl('job_edit', ['id' => $allValues['id']]));
2021-05-21 13:09:48 +02:00
}
$this->addFlash('success', $joboutput['message']);
2022-04-27 14:24:48 +02:00
return new RedirectResponse($this->GenerateUrl('job_index'));
2021-05-21 13:09:48 +02:00
}
}
2022-04-27 14:24:48 +02:00
public function addAction(Request $request, ManagerRegistry $doctrine)
2021-04-12 12:23:50 +02:00
{
2022-04-27 14:24:48 +02:00
if($request->getMethod() == 'GET') {
return $this->render('job/add.html.twig', ['data' => []]);
} elseif ($request->getMethod() == 'POST') {
$allValues = $request->request->all();
$jobRepo = $doctrine->getRepository(Job::class);
2021-05-21 13:09:48 +02:00
try {
$joboutput = $jobRepo->addJob($allValues);
} catch (\InvalidArgumentException $e) {
$this->addFlash('danger', $e->getMessage());
2022-04-27 14:24:48 +02:00
return new RedirectResponse($this->GenerateUrl('job_add'));
2021-04-13 14:07:11 +02:00
}
2021-05-21 13:09:48 +02:00
$this->addFlash('success', $joboutput['message']);
2022-04-27 14:24:48 +02:00
return new RedirectResponse($this->GenerateUrl('job_index'));
2021-04-13 14:07:11 +02:00
} else {
return new Response('Not implemented yet', Response::HTTP_TOO_EARLY);
}
2021-04-12 12:23:50 +02:00
}
2021-06-01 17:41:10 +02:00
2022-04-27 14:24:48 +02:00
public function runNowAction(Request $request, ManagerRegistry $doctrine, int $id) {
if($request->getMethod() == 'GET') {
$jobRepo = $doctrine->getRepository(Job::class);
2021-06-01 17:41:10 +02:00
return new JsonResponse($jobRepo->runNow($id));
}
}
2021-04-06 23:19:51 +02:00
}