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            error_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                    error_log("INFO: Processing quota: " . join(', ', $quota));
30                }
31                if ($commit) {
32                    $this->removeQuota($quota['quotaid']);
33                }
34            }
35        } else {
36            if ($verbose) {
37                error_log("INFO: no expired quota was found");
38            }
39        }
40    }
41
42    protected function removeQuota($quotaId)
43    {
44        $verbose = $this->verbose;
45        if (! $verbose) {
46            $query = new \BO\Zmsdb\Apikey();
47            if ($query->writeDeletedQuota($quotaId)) {
48                if ($verbose) {
49                    error_log("INFO: Quota $quotaId successfully removed");
50                }
51            } else {
52                error_log("WARN: Could not remove quota '$quotaId'!");
53            }
54        } elseif ($verbose) {
55            error_log("INFO: Keep quota $quotaId");
56        }
57    }
58}