Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckTermineSolvIdCommand
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 getAllowedAppEnvs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace Olz\Command;
4
5use Olz\Command\Common\OlzCommand;
6use Symfony\Component\Console\Attribute\AsCommand;
7use Symfony\Component\Console\Command\Command;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Input\InputOption;
10use Symfony\Component\Console\Output\OutputInterface;
11
12#[AsCommand(name: 'olz:check-termine-solv-id')]
13class CheckTermineSolvIdCommand extends OlzCommand {
14    /** @return array<string> */
15    protected function getAllowedAppEnvs(): array {
16        return ['dev', 'test', 'staging', 'prod'];
17    }
18
19    protected function configure(): void {
20        $this->addOption('year', null, InputOption::VALUE_REQUIRED, 'Year');
21        $this->addOption(
22            'future',
23            null,
24            InputOption::VALUE_NONE,
25            'Only run for future Termine.'
26        );
27        $this->addOption(
28            'recent',
29            null,
30            InputOption::VALUE_NONE,
31            'Only run for recently modified Termine.'
32        );
33    }
34
35    protected function handle(InputInterface $input, OutputInterface $output): int {
36        $conditions = [];
37        $year = $input->getOption('year');
38        if ($year) {
39            $sane_year = intval($year);
40            $conditions[] = "YEAR(start_date) = '{$sane_year}'";
41        }
42        $future = $input->getOption('future');
43        if ($future) {
44            $now = $this->dateUtils()->getCurrentDateInFormat('Y-m-d');
45            $conditions[] = "start_date > '{$now}'";
46        }
47        $recent = $input->getOption('recent');
48        if ($recent) {
49            $now = $this->dateUtils()->getCurrentDateInFormat('Y-m-d');
50            $recently = date('Y-m-d H:i:s', (strtotime($now) ?: 0) - 86400 - 3600);
51            $conditions[] = "last_modified_at > '{$recently}'";
52        }
53
54        $db = $this->dbUtils()->getDb();
55        $sql_where = implode(' AND ', $conditions);
56        $this->logAndOutput("Running with {$sql_where}");
57        $result = $db->query("SELECT * FROM termine WHERE {$sql_where} AND solv_uid IS NULL");
58        // @phpstan-ignore-next-line
59        while ($row = $result->fetch_assoc()) {
60            $start_date = $row['start_date']; // TODO: Improve precision for multi-day events?
61            $title = $row['title'];
62            $id = $row['id'];
63            $result_solv = $db->query("SELECT * FROM solv_events WHERE `date` = '{$start_date}'");
64            $this->logAndOutput("Termin: {$start_date} {$title} ({$id})");
65            // @phpstan-ignore-next-line
66            while ($row_solv = $result_solv->fetch_assoc()) {
67                $solv_date = $row_solv['date'];
68                $solv_name = $row_solv['name'];
69                $solv_uid = $row_solv['solv_uid'];
70                // @phpstan-ignore-next-line
71                $levenshtein = levenshtein($title, $solv_name, 1, 2, 1);
72                // @phpstan-ignore-next-line
73                $num_same = strlen($title) + strlen($solv_name) - $levenshtein;
74                $this->logAndOutput("  SOLV-Event: {$solv_date} {$solv_name} ({$solv_uid}) - Diff: {$num_same} (LEV: {$levenshtein})");
75            }
76        }
77        return Command::SUCCESS;
78    }
79}