43 lines
1.6 KiB
PHP
43 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tempo\Inventory\DeliveryReturnWriter;
|
|
|
|
final class WarehouseReturnMovement
|
|
{
|
|
public const MANIFEST_SHA256='2850a4f5cb678acfe3a9dbc7a7d62eb233bc72722ad0de374681c2679c90557c';
|
|
private function fileHash(string $file): string
|
|
{
|
|
$hash = is_file($file) ? hash_file('sha256', $file) : false;
|
|
abort_unless(is_string($hash), 503, 'Ficheiro do pacote de devolução indisponível.');
|
|
|
|
return $hash;
|
|
}
|
|
|
|
public function for(User $actor): DeliveryReturnWriter
|
|
{
|
|
$root = base_path('packages/inventory-return/releases/0.1.0');
|
|
if (! hash_equals(self::MANIFEST_SHA256, $this->fileHash($root.'/manifest.json'))) {
|
|
abort(503, 'Pacote de devolução incompatível.');
|
|
}
|
|
$raw = file_get_contents($root.'/manifest.json');
|
|
if ($raw === false) {
|
|
abort(503, 'Manifesto de devolução indisponível.');
|
|
}
|
|
$manifest = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
|
foreach (['bootstrap.php', 'src/DeliveryReturnWriter.php'] as $file) {
|
|
if (! hash_equals($manifest['files'][$file] ?? '', $this->fileHash($root.'/'.$file))) {
|
|
abort(503, 'Pacote de devolução alterado.');
|
|
}
|
|
}
|
|
require_once $root.'/bootstrap.php';
|
|
|
|
return new DeliveryReturnWriter(DB::connection(), 'public', (int) $actor->tenant_id, (int) $actor->id, function () use ($actor) {
|
|
app(WarehouseReturns::class)->authorize($actor, 'confirm');
|
|
});
|
|
}
|
|
}
|