-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckFileHashesCommand.php
More file actions
88 lines (73 loc) · 2.91 KB
/
CheckFileHashesCommand.php
File metadata and controls
88 lines (73 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Gared\EtherScan\Console;
use Gared\EtherScan\Model\VersionRange;
use Gared\EtherScan\Service\FileHashLookupService;
use Gared\EtherScan\Service\StaticFileClient;
use GuzzleHttp\Client;
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;
#[AsCommand(
name: 'ether:check-file-hashes',
description: 'Check file hashes of given instance'
)]
class CheckFileHashesCommand extends Command
{
protected function configure(): void
{
$this
->addArgument('url', InputArgument::REQUIRED, 'Url to etherpad instance')
->addArgument('version', InputArgument::REQUIRED, 'Etherpad version');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$url = $input->getArgument('url');
$version = $input->getArgument('version');
$fileHashLookup = new FileHashLookupService();
$staticFileClient = new StaticFileClient(new Client());
$files = FileHashLookupService::getFileNames();
$versionRanges = [];
foreach ($files as $file) {
$fileHash = $staticFileClient->getFileHash($url, $file);
$versionRange = $fileHashLookup->getEtherpadVersionRange($file, $fileHash);
if ($versionRange !== null) {
$versionRanges[] = $versionRange;
}
}
$versionRange = $this->calculateVersion($versionRanges);
$output->writeln('Calculated version range: ' . $versionRange->__toString());
if (
(
($versionRange->getMinVersion() === null || version_compare($versionRange->getMinVersion(), $version, '<=')) &&
($versionRange->getMaxVersion() === null || version_compare($versionRange->getMaxVersion(), $version, '>='))
) === false
) {
$output->writeln('Version mismatch');
return self::FAILURE;
}
return self::SUCCESS;
}
/**
* @param list<VersionRange> $versionRanges
*/
private function calculateVersion(array $versionRanges): VersionRange
{
if (count($versionRanges) === 0) {
throw new \Exception('No version ranges found');
}
$maxVersion = null;
$minVersion = null;
foreach ($versionRanges as $version) {
if ($maxVersion === null || version_compare($version->getMaxVersion() ?? '', $maxVersion, '<')) {
$maxVersion = $version->getMaxVersion();
}
if ($minVersion === null || version_compare($version->getMinVersion() ?? '', $minVersion, '>')) {
$minVersion = $version->getMinVersion();
}
}
return new VersionRange($minVersion, $maxVersion);
}
}