69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Api\Webdav\Client;
|
|
use App\Repository\BillRepositoryQuery;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
#[AsCommand(
|
|
name: 'webdav:bill:sync',
|
|
description: 'Sync bills',
|
|
)]
|
|
class WebdavBillSyncCommand extends Command
|
|
{
|
|
protected Client $client;
|
|
protected BillRepositoryQuery $query;
|
|
|
|
public function __construct(Client $client, BillRepositoryQuery $query)
|
|
{
|
|
$this->client = $client;
|
|
$this->query = $query;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
chdir(__DIR__.'/../../public');
|
|
|
|
$remoteFiles = $this->client->ls('/');
|
|
$localFiles = [];
|
|
|
|
foreach ($this->query->find() as $entity) {
|
|
$localFiles[basename($entity->getFile())] = $entity->getFile();
|
|
}
|
|
|
|
foreach ($localFiles as $basename => $file) {
|
|
if (!$this->client->exists($basename)) {
|
|
$this->client->sendFile($file, $basename);
|
|
$output->writeln(sprintf(
|
|
'Fichier <comment>%s</comment> envoyé',
|
|
$file
|
|
));
|
|
}
|
|
}
|
|
|
|
$isFirst = true;
|
|
|
|
foreach ($remoteFiles as $remoteFile) {
|
|
$name = $remoteFile['{DAV:}displayname'];
|
|
|
|
if (!isset($localFiles[$name]) && !$isFirst) {
|
|
$this->client->rm($name);
|
|
$output->writeln(sprintf(
|
|
'Fichier distant <comment>%s</comment> supprimé',
|
|
$name
|
|
));
|
|
}
|
|
|
|
$isFirst = false;
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|