fix: moves monitor logic into own directory
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Download\Framework\Controller;
|
||||
|
||||
use App\Download\Action\Handler\AddMonitorHandler;
|
||||
use App\Download\Action\Input\AddMonitorInput;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Mercure\HubInterface;
|
||||
use Symfony\Component\Mercure\Update;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Twig\Environment;
|
||||
|
||||
class ApiController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'twig')]
|
||||
private readonly Environment $renderer,
|
||||
private readonly HubInterface $hub,
|
||||
) {}
|
||||
|
||||
#[Route('/api/monitor', name: 'api_monitor', methods: ['POST'])]
|
||||
public function addMonitor(
|
||||
AddMonitorInput $input,
|
||||
AddMonitorHandler $handler,
|
||||
HubInterface $hub,
|
||||
) {
|
||||
$response = $handler->handle($input->toCommand());
|
||||
|
||||
$hub->publish(new Update(
|
||||
'alerts',
|
||||
$this->renderer->render('broadcast/Alert.html.twig', [
|
||||
'alert_id' => uniqid(),
|
||||
'title' => 'Success',
|
||||
'message' => "New monitor added for {$input->title}",
|
||||
])
|
||||
));
|
||||
|
||||
return $this->json([
|
||||
'status' => 200,
|
||||
'message' => $response
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Download\Framework\Entity;
|
||||
|
||||
use App\Download\Framework\Repository\MonitorRepository;
|
||||
use App\User\Framework\Entity\User;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Ignore;
|
||||
use Symfony\Component\Serializer\Attribute\MaxDepth;
|
||||
|
||||
#[ORM\Entity(repositoryClass: MonitorRepository::class)]
|
||||
class Monitor
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[Ignore]
|
||||
#[ORM\ManyToOne(inversedBy: 'yes')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?User $user = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
private ?string $title = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $imdbId = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $tmdbId = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $monitorType = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $season = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $episode = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $status = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $searchCount = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
|
||||
private ?\DateTimeInterface $lastSearch = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private ?\DateTimeImmutable $createdAt = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?\DateTimeImmutable $downloadedAt = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setUser(?User $user): static
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTitle(): ?string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle(?string $title): static
|
||||
{
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getImdbId(): ?string
|
||||
{
|
||||
return $this->imdbId;
|
||||
}
|
||||
|
||||
public function setImdbId(string $imdbId): static
|
||||
{
|
||||
$this->imdbId = $imdbId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTmdbId(): ?string
|
||||
{
|
||||
return $this->tmdbId;
|
||||
}
|
||||
|
||||
public function setTmdbId(string $tmdbId): static
|
||||
{
|
||||
$this->tmdbId = $tmdbId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatus(): ?string
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus(string $status): static
|
||||
{
|
||||
$this->status = $status;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSearchCount(): ?int
|
||||
{
|
||||
return $this->searchCount;
|
||||
}
|
||||
|
||||
public function setSearchCount(?int $searchCount): static
|
||||
{
|
||||
$this->searchCount = $searchCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLastSearch(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->lastSearch;
|
||||
}
|
||||
|
||||
public function setLastSearch(?\DateTimeInterface $lastSearch): static
|
||||
{
|
||||
$this->lastSearch = $lastSearch;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeImmutable $createdAt): static
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDownloadedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->downloadedAt;
|
||||
}
|
||||
|
||||
public function setDownloadedAt(?\DateTimeImmutable $downloadedAt): static
|
||||
{
|
||||
$this->downloadedAt = $downloadedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMonitorType(): ?string
|
||||
{
|
||||
return $this->monitorType;
|
||||
}
|
||||
|
||||
public function setMonitorType(string $monitorType): static
|
||||
{
|
||||
$this->monitorType = $monitorType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSeason(): ?int
|
||||
{
|
||||
return $this->season;
|
||||
}
|
||||
|
||||
public function setSeason(?int $season): static
|
||||
{
|
||||
$this->season = $season;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEpisode(): ?int
|
||||
{
|
||||
return $this->episode;
|
||||
}
|
||||
|
||||
public function setEpisode(?int $episode): static
|
||||
{
|
||||
$this->episode = $episode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Download\Framework\Repository;
|
||||
|
||||
use App\Download\Framework\Entity\Monitor;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Monitor>
|
||||
*/
|
||||
class MonitorRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Monitor::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return MovieMonitor[] Returns an array of MovieMonitor objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('m.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?MovieMonitor
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Download\Framework\Scheduler;
|
||||
|
||||
use App\Download\Action\Command\MonitorMovieCommand;
|
||||
use App\Download\Action\Command\MonitorTvEpisodeCommand;
|
||||
use App\Download\Action\Command\MonitorTvSeasonCommand;
|
||||
use App\Download\Action\Command\MonitorTvShowCommand;
|
||||
use App\Download\Framework\Repository\MonitorRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Scheduler\Attribute\AsCronTask;
|
||||
|
||||
#[AsCronTask('*/10 * * * *', schedule: 'monitor')]
|
||||
class MonitorDispatcher
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly MonitorRepository $monitorRepository,
|
||||
private readonly MessageBusInterface $bus,
|
||||
) {}
|
||||
|
||||
public function __invoke() {
|
||||
$this->logger->info('[MonitorDispatcher] Executing MonitorDispatcher');
|
||||
|
||||
$monitorHandlers = [
|
||||
'movie' => MonitorMovieCommand::class,
|
||||
'tvepisode' => MonitorTvEpisodeCommand::class,
|
||||
'tvseason' => MonitorTvSeasonCommand::class,
|
||||
'tvshow' => MonitorTvShowCommand::class,
|
||||
];
|
||||
|
||||
$monitors = $this->monitorRepository->findBy(['status' => ['New', 'Active']]);
|
||||
|
||||
foreach ($monitors as $monitor) {
|
||||
$monitor->setStatus('In Progress');
|
||||
$this->monitorRepository->getEntityManager()->flush();
|
||||
|
||||
$command = $monitorHandlers[$monitor->getMonitorType()];
|
||||
$this->logger->info('[MonitorDispatcher] Dispatching ' . $command . ' for ' . $monitor->getTitle());
|
||||
$this->bus->dispatch(new $command($monitor->getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user