Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OnDailyEndpoint
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 parseInput
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 shouldFailThrottling
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 handle
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Olz\Api\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Entity\Throttling;
7use PhpTypeScriptApi\HttpError;
8use Symfony\Component\Console\Input\ArrayInput;
9use Symfony\Component\Console\Output\BufferedOutput;
10use Symfony\Component\HttpFoundation\Request;
11
12/**
13 * @extends OlzTypedEndpoint<
14 *   array{
15 *     authenticityCode: non-empty-string,
16 *   },
17 *   ?array{}
18 * >
19 */
20class OnDailyEndpoint extends OlzTypedEndpoint {
21    public function parseInput(Request $request): mixed {
22        return [
23            'authenticityCode' => $request->query->get('authenticityCode'),
24        ];
25    }
26
27    public function shouldFailThrottling(): bool {
28        if ($this->envUtils()->hasUnlimitedCron()) {
29            return false;
30        }
31        $throttling_repo = $this->entityManager()->getRepository(Throttling::class);
32        $last_daily = $throttling_repo->getLastOccurrenceOf('on_daily');
33        if (!$last_daily) {
34            return false;
35        }
36        $now = new \DateTime($this->dateUtils()->getIsoNow());
37        $min_interval = \DateInterval::createFromDateString('+22 hours');
38        $min_now = $last_daily->add($min_interval);
39        return $now < $min_now;
40    }
41
42    protected function handle(mixed $input): mixed {
43        $expected_code = $this->envUtils()->getCronAuthenticityCode();
44        $actual_code = $input['authenticityCode'];
45        if ($actual_code != $expected_code) {
46            throw new HttpError(403, "Kein Zugriff!");
47        }
48
49        set_time_limit(4000);
50        ignore_user_abort(true);
51
52        $throttling_repo = $this->entityManager()->getRepository(Throttling::class);
53        $throttling_repo->recordOccurrenceOf('on_daily', $this->dateUtils()->getIsoNow());
54
55        $command_input = new ArrayInput([]);
56        $command_output = new BufferedOutput();
57        $this->symfonyUtils()->callCommand('olz:on-daily', $command_input, $command_output);
58
59        return [];
60    }
61}