Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
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\Zmsdb\Helper;
4
5/**
6 * @codeCoverageIgnore
7 */
8class ApiQuotaDeleteByCron
9{
10    protected $quotaList;
11    protected $verbose = false;
12
13    public function __construct(\DateTimeInterface $dateTime, $verbose = false)
14    {
15        $query = new \BO\Zmsdb\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($commit)
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)
41    {
42        $verbose = $this->verbose;
43        if (! $verbose) {
44            $query = new \BO\Zmsdb\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}