CluPilotCloud/app/Console/Commands/PublishProcessingAgreement.php

70 lines
2.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\DpaVersion;
use App\Services\Legal\DpaRenderer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
/**
* Render the processing agreement and its annex, store them, put them in force.
*
* The documents are generated from the repository (resources/views/legal/dpa),
* so a change to the wording is a change somebody can review in a diff — and the
* company name, the sub-processors and the deletion deadlines come from the same
* places the rest of the application reads them, which is what stops the
* agreement drifting away from what the software does.
*
* The console's upload form still exists and is unchanged: a lawyer's revision
* arrives as a PDF and becomes a version like any other. This command is for the
* version this application maintains itself.
*/
class PublishProcessingAgreement extends Command
{
protected $signature = 'clupilot:publish-dpa
{version : What the document calls itself, e.g. 1.0}
{--draft : Store it without putting it in force}';
protected $description = 'Render the processing agreement and its annex, and put them in force';
public function handle(DpaRenderer $renderer): int
{
$version = (string) $this->argument('version');
if (DpaVersion::query()->where('version', $version)->exists()) {
$this->error("Version {$version} already exists. Pick another name — two documents under one name makes every acceptance ambiguous.");
return self::FAILURE;
}
$agreementPath = 'dpa/av-vertrag-'.$this->slug($version).'.pdf';
$measuresPath = 'dpa/tom-'.$this->slug($version).'.pdf';
Storage::disk('local')->put($agreementPath, $renderer->agreement($version));
Storage::disk('local')->put($measuresPath, $renderer->measures($version));
$row = DpaVersion::query()->create([
'version' => $version,
'agreement_path' => $agreementPath,
'measures_path' => $measuresPath,
'note' => 'Aus dem Repository erzeugt (clupilot:publish-dpa).',
// Publishing is the point of the command; --draft is for looking at
// it first. Every customer who accepted an earlier version is
// outstanding again from this moment.
'published_at' => $this->option('draft') ? null : now(),
]);
$this->info(($this->option('draft') ? 'Drafted' : 'Published').' version '.$row->version.'.');
$this->line(' '.$agreementPath.' ('.number_format(strlen(Storage::disk('local')->get($agreementPath)) / 1024, 0).' KB)');
$this->line(' '.$measuresPath.' ('.number_format(strlen(Storage::disk('local')->get($measuresPath)) / 1024, 0).' KB)');
return self::SUCCESS;
}
private function slug(string $version): string
{
return preg_replace('/[^a-z0-9.-]+/i', '-', $version) ?: 'version';
}
}