| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\Report; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Core; |
| 26 |
use Liuch\DmarcSrg\Domains\Domain; |
| 27 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 28 |
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 29 |
|
| 30 |
class Report |
| 31 |
{ |
| 32 |
private $db = null; |
| 33 |
private $data = null; |
| 34 |
|
| 35 |
public function __construct($data, $db = null) |
| 36 |
{ |
| 37 |
if (is_array($data)) { |
| 38 |
$domain = $data['domain']; |
| 39 |
$data = ReportData::fromArray($data); |
| 40 |
} else { |
| 41 |
$domain = $data->domain; |
| 42 |
} |
| 43 |
if ($domain instanceof Domain) { |
| 44 |
$data->domain = $domain->fqdn(); |
| 45 |
} |
| 46 |
$this->data = $data; |
| 47 |
$this->db = $db ?? Core::instance()->database(); |
| 48 |
} |
| 49 |
|
| 50 |
public static function fromXmlFile($fd) |
| 51 |
{ |
| 52 |
$data = ReportData::fromXmlFile($fd); |
| 53 |
if (!$data->isValid()) { |
| 54 |
throw new SoftException('Incorrect or incomplete report data'); |
| 55 |
} |
| 56 |
return new Report($data); |
| 57 |
} |
| 58 |
|
| 59 |
public function fetch() |
| 60 |
{ |
| 61 |
$this->prepareData(true); |
| 62 |
try { |
| 63 |
$this->db->getMapper('report')->fetch($this->data); |
| 64 |
} catch (DatabaseNotFoundException $e) { |
| 65 |
throw new SoftException('Report not found'); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public function save(string $real_fname) |
| 70 |
{ |
| 71 |
$b_ts = $this->data->date['begin']; |
| 72 |
$e_ts = $this->data->date['end']; |
| 73 |
if (!$b_ts->getTimestamp() || !$e_ts->getTimestamp() |
| 74 |
|| strlen($b_ts->format('Y')) !== 4 || strlen($e_ts->format('Y')) !== 4 |
| 75 |
) { |
| 76 |
throw new SoftException('Failed to add an incoming report: wrong date value'); |
| 77 |
} |
| 78 |
if ($b_ts > $e_ts) { |
| 79 |
throw new SoftException('Failed to add an incoming report: start date is later than end date'); |
| 80 |
} |
| 81 |
|
| 82 |
$this->db->getMapper('report')->save($this->data); |
| 83 |
return [ 'message' => 'The report is loaded successfully' ]; |
| 84 |
} |
| 85 |
|
| 86 |
public function __get(string $name) |
| 87 |
{ |
| 88 |
return $this->data->$name; |
| 89 |
} |
| 90 |
|
| 91 |
public function toArray(): array |
| 92 |
{ |
| 93 |
return $this->data->toArray(); |
| 94 |
} |
| 95 |
|
| 96 |
public function set($name, $value) |
| 97 |
{ |
| 98 |
$this->prepareData(false); |
| 99 |
$this->db->getMapper('report')->setProperty($this->data, $name, $value); |
| 100 |
return [ 'message' => 'Ok' ]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private function prepareData(bool $replace): void |
| 111 |
{ |
| 112 |
$data = []; |
| 113 |
$ne_f = false; |
| 114 |
foreach ([ 'domain', 'date', 'org_name', 'report_id'] as $fld) { |
| 115 |
if (empty($this->data->$fld)) { |
| 116 |
$ne_f = true; |
| 117 |
break; |
| 118 |
} |
| 119 |
if ($replace) { |
| 120 |
$data[$fld] = $this->data->$fld; |
| 121 |
} |
| 122 |
} |
| 123 |
if ($ne_f || empty($this->data->date['begin'])) { |
| 124 |
throw new SoftException('Not enough data to identify the report'); |
| 125 |
} |
| 126 |
if ($replace) { |
| 127 |
$this->data = ReportData::fromArray($data); |
| 128 |
} |
| 129 |
} |
| 130 |
} |