Lines n/a 0 / 0
Functions and Methods n/a 0 / 0
Classes and Traits n/a 0 / 0
Name Lines Functions and Methods CRAP Classes and Traits
ApiQuotaDeleteByCron n/a 0 / 0 n/a 0 / 0 13 n/a 0 / 0
 __construct n/a 0 / 0 n/a 0 / 0 2
 startProcessing n/a 0 / 0 n/a 0 / 0 6
 removeQuota n/a 0 / 0 n/a 0 / 0 5
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5/**
6 * @codeCoverageIgnore
7 */
8class ApiQuotaDeleteByCron
9{
10    protected $quotaList;
11    protected $verbose = false;
12
13    public function __construct(\DateTimeInterface $dateTime, bool $verbose = false)
14    {
15        $query = new \BO\Zmsbackend\Apikey\Service\Apikey();
16        if ($verbose) {
17            \App::$log->info('Deleting quota older than given period');
18            $this->verbose = true;
19        }
20        $this->quotaList = $query->readExpiredQuotaListByPeriod($dateTime);
21    }
22
23    public function startProcessing(bool $commit): void
24    {
25        $verbose = $this->verbose;
26        if ($this->quotaList) {
27            foreach ($this->quotaList as $quota) {
28                if ($verbose) {
29                    \App::$log->info('Processing quota', ['quota' => join(', ', $quota)]);
30                }
31                if ($commit) {
32                    $this->removeQuota($quota['quotaid']);
33                }
34            }
35        } elseif ($verbose) {
36            \App::$log->info('No expired quota was found');
37        }
38    }
39
40    protected function removeQuota($quotaId): void
41    {
42        $verbose = $this->verbose;
43        if (! $verbose) {
44            $query = new \BO\Zmsbackend\Apikey\Service\Apikey();
45            if ($query->writeDeletedQuota($quotaId)) {
46                if ($verbose) {
47                    \App::$log->info('Quota successfully removed', ['quotaId' => $quotaId]);
48                }
49            } else {
50                \App::$log->warning('Could not remove quota', ['quotaId' => $quotaId]);
51            }
52        } elseif ($verbose) {
53            \App::$log->info('Keep quota', ['quotaId' => $quotaId]);
54        }
55    }
56}