Cleanup of cli
This commit is contained in:
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Command;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Common\Service\Communicator;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'attachments:decrypt',
|
||||
description: 'Decrypt attachments pending',
|
||||
hidden: false
|
||||
)]
|
||||
class DecryptPdf extends Command
|
||||
{
|
||||
public function __construct(Communicator $communicator, string $name = null)
|
||||
{
|
||||
$this->setCommunicator($communicator);
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected Communicator $communicator;
|
||||
public function getCommunicator(): Communicator
|
||||
{
|
||||
return $this->communicator;
|
||||
}
|
||||
public function setCommunicator(Communicator $communicator): DecryptPdf
|
||||
{
|
||||
$this->communicator = $communicator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getAttachments(): array
|
||||
{
|
||||
$response = $this->getCommunicator()->get('/attachments/pending');
|
||||
return json_decode($response->getBody()->getContents())->attachments;
|
||||
}
|
||||
protected function decrypt(string $attachment): bool
|
||||
{
|
||||
$response = $this->getCommunicator()->put('/attachments/decrypt', ['attachments' => [$attachment]]);
|
||||
return json_decode($response->getBody()->getContents())->status;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Decrypt Attachments');
|
||||
|
||||
$io->section('Grabbing Attachments');
|
||||
$attachments = $this->getAttachments();
|
||||
$io->text('Found ' . count($attachments) . ' attachments.');
|
||||
$io->section('Decrypting Attachments');
|
||||
foreach ($attachments as $attachment) {
|
||||
$status = $this->decrypt($attachment);
|
||||
if ($status) {
|
||||
$io->success("{$attachment} decrypted correctly.");
|
||||
} else {
|
||||
$io->error("Problem decrypting {$attachment}.");
|
||||
}
|
||||
}
|
||||
$io->success('Done.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Command;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Common\Service\Communicator;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'attachments:grab',
|
||||
description: 'Grab attachments from pending messages',
|
||||
aliases: ['attachments:get'],
|
||||
hidden: false
|
||||
)]
|
||||
class GrabAttachments extends Command
|
||||
{
|
||||
public function __construct(Communicator $communicator, string $name = null)
|
||||
{
|
||||
$this->setCommunicator($communicator);
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected Communicator $service;
|
||||
public function getCommunicator(): Communicator
|
||||
{
|
||||
return $this->service;
|
||||
}
|
||||
public function setCommunicator(Communicator $service): GrabAttachments
|
||||
{
|
||||
$this->service = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getMessages(): array
|
||||
{
|
||||
$response = $this->getCommunicator()->get('/messages/pending');
|
||||
return json_decode($response->getBody()->getContents())->messages;
|
||||
}
|
||||
protected function grabAttachments(int $message_uid): int
|
||||
{
|
||||
$response = $this->getCommunicator()->put('/attachments/grab', ['messages' => [$message_uid]]);
|
||||
return json_decode($response->getBody()->getContents())->attachment_count;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Grab Attachments');
|
||||
|
||||
$io->section('Grabbing Messages');
|
||||
$messages = $this->getMessages();
|
||||
$io->text('Found ' . count($messages) . ' messages.');
|
||||
$io->section('Grabbing Attachments');
|
||||
foreach ($messages as $job) {
|
||||
$message = $job->message;
|
||||
$attachments = $this->grabAttachments($message->uid);
|
||||
$io->text("Found {$attachments} attachments for message UID:{$message->uid}.");
|
||||
}
|
||||
$io->success('Done.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
79
cli/common/Command/Jobs/Check.php
Normal file
79
cli/common/Command/Jobs/Check.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace ProVM\Command\Jobs;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Service\Communicator;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'jobs:check',
|
||||
description: 'Check for pending jobs',
|
||||
hidden: false
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function getPendingJobs(): array
|
||||
{
|
||||
$this->logger->notice('Grabbing pending jobs.');
|
||||
$response = $this->communicator->get('/jobs/pending');
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return [];
|
||||
}
|
||||
return json_decode($body)->jobs;
|
||||
}
|
||||
protected function runJob($job): bool
|
||||
{
|
||||
$base_command = '/app/bin/emails';
|
||||
$cmd = [$base_command, $job->command];
|
||||
if ($job->arguments !== '') {
|
||||
$cmd []= $job->arguments;
|
||||
}
|
||||
$cmd = implode(' ', $cmd);
|
||||
$this->logger->notice("Running '{$cmd}'");
|
||||
$response = shell_exec($cmd);
|
||||
$this->logger->info("Result: {$response}");
|
||||
return $response !== false;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$section1 = $output->section();
|
||||
$section2 = $output->section();
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking Pending Jobs');
|
||||
$pending_jobs = $this->getPendingJobs();
|
||||
$notice = 'Found ' . count($pending_jobs) . ' jobs';
|
||||
$io1->text($notice);
|
||||
$this->logger->info($notice);
|
||||
if (count($pending_jobs) > 0) {
|
||||
$io1->section('Running Jobs');
|
||||
$io1->progressStart(count($pending_jobs));
|
||||
foreach ($pending_jobs as $job) {
|
||||
$section2->clear();
|
||||
$io2->text("Running {$job->command}");
|
||||
if ($this->runJob($job)) {
|
||||
$io2->success('Success');
|
||||
} else {
|
||||
$io2->error('Failure');
|
||||
}
|
||||
$io1->progressAdvance();
|
||||
}
|
||||
}
|
||||
$section2->clear();
|
||||
$io2->success('Done');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
76
cli/common/Command/Mailboxes/Check.php
Normal file
76
cli/common/Command/Mailboxes/Check.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace ProVM\Command\Mailboxes;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ProVM\Service\Communicator;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mailboxes:check',
|
||||
description: 'Check registered mailboxes for new emails',
|
||||
hidden: false
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected int $min_check_days, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function getMailboxes(): array
|
||||
{
|
||||
$response = $this->communicator->get('/mailboxes/registered');
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return [];
|
||||
}
|
||||
return json_decode($body)->mailboxes;
|
||||
}
|
||||
protected function checkMailbox($mailbox): bool
|
||||
{
|
||||
if ((new \DateTimeImmutable())->diff(new \DateTimeImmutable($mailbox->last_checked->date->date))->days < $this->min_check_days) {
|
||||
return true;
|
||||
}
|
||||
$response = $this->communicator->get("/mailbox/{$mailbox->id}/check");
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return true;
|
||||
}
|
||||
return json_decode($body)->status;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$section1 = $output->section();
|
||||
$section2 = $output->section();
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking for New Messages');
|
||||
$mailboxes = $this->getMailboxes();
|
||||
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
|
||||
$io1->text($notice);
|
||||
if (count($mailboxes) > 0) {
|
||||
$io1->section('Checking for new messages');
|
||||
$io1->progressStart(count($mailboxes));
|
||||
foreach ($mailboxes as $mailbox) {
|
||||
$section2->clear();
|
||||
$io2->text("Checking {$mailbox->name}");
|
||||
if ($this->checkMailbox($mailbox)) {
|
||||
$io2->success("Found new emails in {$mailbox->name}");
|
||||
} else {
|
||||
$io2->info("No new emails in {$mailbox->name}");
|
||||
}
|
||||
$io1->progressAdvance();
|
||||
}
|
||||
$io1->progressFinish();
|
||||
}
|
||||
$section2->clear();
|
||||
$io2->success('Done');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -1,65 +1,62 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Command;
|
||||
namespace ProVM\Command\Messages;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Common\Service\Communicator;
|
||||
use ProVM\Service\Communicator;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'messages:grab',
|
||||
description: 'Run grab messages job for registered mailboxes',
|
||||
aliases: ['messages'],
|
||||
hidden: false
|
||||
)]
|
||||
class Messages extends Command
|
||||
class Grab extends Command
|
||||
{
|
||||
public function __construct(Communicator $communicator, string $name = null)
|
||||
{
|
||||
$this->setCommunicator($communicator);
|
||||
parent::__construct($name);
|
||||
}
|
||||
protected function configure()
|
||||
{
|
||||
$this->addArgument('mailbox_id', InputArgument::REQUIRED, 'Mailbox ID to grab emails');
|
||||
}
|
||||
|
||||
protected Communicator $communicator;
|
||||
public function getCommunicator(): Communicator
|
||||
{
|
||||
return $this->communicator;
|
||||
}
|
||||
public function setCommunicator(Communicator $communicator): Messages
|
||||
public function setCommunicator(Communicator $communicator): Grab
|
||||
{
|
||||
$this->communicator = $communicator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getMailboxes(): array
|
||||
protected function grabMessages(int $mailbox_id): int
|
||||
{
|
||||
$response = $this->getCommunicator()->get('/mailboxes/registered');
|
||||
return json_decode($response->getBody()->getContents())->mailboxes;
|
||||
}
|
||||
protected function grabMessages(string $mailbox): int
|
||||
{
|
||||
$response = $this->getCommunicator()->put('/messages/grab', ['mailboxes' => [$mailbox]]);
|
||||
$body = json_decode($response->getBody()->getContents());
|
||||
return $body->message_count;
|
||||
$response = $this->getCommunicator()->get("/mailbox/{$mailbox_id}/grab");
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return 0;
|
||||
}
|
||||
return json_decode($body)->messages->count;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$io->title('Messages');
|
||||
$io->section('Grabbing Registered Mailboxes');
|
||||
|
||||
$mailboxes = $this->getMailboxes();
|
||||
$io->text('Found ' . count($mailboxes) . ' registered mailboxes.');
|
||||
$mailbox_id = $input->getArgument('mailbox_id');
|
||||
$io->title("Grabbing Messages for Mailbox ID {$mailbox_id}");
|
||||
$io->section('Grabbing Messages');
|
||||
foreach ($mailboxes as $mailbox) {
|
||||
$message_count = $this->grabMessages($mailbox->name);
|
||||
$io->text("Found {$message_count} messages in {$mailbox->name}.");
|
||||
}
|
||||
$count = $this->grabMessages($mailbox_id);
|
||||
$io->info("Found {$count} messages");
|
||||
$io->success('Done.');
|
||||
|
||||
return Command::SUCCESS;
|
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Middleware;
|
||||
namespace ProVM\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
namespace ProVM\Service;
|
||||
|
||||
use HttpResponseException;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Safe\Exceptions\JsonException;
|
||||
use function Safe\json_encode;
|
||||
|
||||
class Communicator
|
||||
@ -26,6 +28,9 @@ class Communicator
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
*/
|
||||
protected function handleResponse(ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) {
|
||||
@ -33,6 +38,11 @@ class Communicator
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function request(string $method, string $uri, ?array $body = null): ResponseInterface
|
||||
{
|
||||
$options = [];
|
||||
@ -45,20 +55,39 @@ class Communicator
|
||||
return $this->handleResponse($this->getClient()->request($method, $uri, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function get(string $uri): ResponseInterface
|
||||
{
|
||||
return $this->request('get', $uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function post(string $uri, array $data): ResponseInterface
|
||||
{
|
||||
return $this->request('post', $uri, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function put(string $uri, array $data): ResponseInterface
|
||||
{
|
||||
return $this->request('put', $uri, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpResponseException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function delete(string $uri, array $data): ResponseInterface
|
||||
{
|
||||
return $this->request('delete', $uri, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Wrapper;
|
||||
namespace ProVM\Wrapper;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Application as Base;
|
||||
@ -22,4 +22,4 @@ class Application extends Base
|
||||
$this->container = $container;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user