|
| 1 | +<?php |
| 2 | +namespace pmill\Plesk; |
| 3 | + |
| 4 | +use pmill\Plesk\Helper\Xml; |
| 5 | + |
| 6 | +class GetTraffic extends BaseRequest |
| 7 | +{ |
| 8 | + /** |
| 9 | + * @var string |
| 10 | + */ |
| 11 | + public $xml_packet = <<<EOT |
| 12 | +<?xml version="1.0"?> |
| 13 | +<packet version="1.6.3.0"> |
| 14 | +<webspace> |
| 15 | + <get_traffic> |
| 16 | + {FILTER} |
| 17 | + </get_traffic> |
| 18 | +</webspace> |
| 19 | +</packet> |
| 20 | +EOT; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + protected $default_params = [ |
| 26 | + 'filter' => null, |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param array $config |
| 31 | + * @param array $params |
| 32 | + * @throws ApiRequestException |
| 33 | + */ |
| 34 | + public function __construct($config, $params = []) |
| 35 | + { |
| 36 | + $filterChildNodes = []; |
| 37 | + |
| 38 | + foreach (['name', 'owner-id', 'owner-login', 'guid', 'id'] as $nodeName) { |
| 39 | + if (isset($params[$nodeName])) { |
| 40 | + if (!is_array($params[$nodeName])) { |
| 41 | + $params[$nodeName] = [$params[$nodeName]]; |
| 42 | + } |
| 43 | + |
| 44 | + foreach ($params[$nodeName] as $value) { |
| 45 | + $filterChildNodes[] = new Node($nodeName, $value); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $filter = [ new Node('filter', new NodeList($filterChildNodes)) ]; |
| 51 | + |
| 52 | + if (isset($params['since_date'])) { |
| 53 | + $filter[] = new Node('since_date', $params['since_date']); |
| 54 | + } |
| 55 | + |
| 56 | + if (isset($params['to_date'])) { |
| 57 | + $filter[] = new Node('to_date', $params['to_date']); |
| 58 | + } |
| 59 | + |
| 60 | + $params = [ |
| 61 | + 'filter' => new NodeList($filter) |
| 62 | + ]; |
| 63 | + |
| 64 | + parent::__construct($config, $params); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param $xml |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + protected function processResponse($xml) |
| 72 | + { |
| 73 | + $result = []; |
| 74 | + |
| 75 | + for ($i = 0; $i < count($xml->webspace->get_traffic->result); $i++) { |
| 76 | + $webspace = $xml->webspace->get_traffic->result[$i]; |
| 77 | + |
| 78 | + $traffic = []; |
| 79 | + foreach ($webspace->traffic as $day) { |
| 80 | + $traffic[] = [ |
| 81 | + 'date' => (string)$day->date, |
| 82 | + 'http_in' => (int)$day->http_in, |
| 83 | + 'http_out' => (int)$day->http_out, |
| 84 | + 'ftp_in' => (int)$day->ftp_in, |
| 85 | + 'ftp_out' => (int)$day->ftp_out, |
| 86 | + 'smtp_in' => (int)$day->smtp_in, |
| 87 | + 'smtp_out' => (int)$day->smtp_out, |
| 88 | + 'pop3_imap_in' => (int)$day->pop3_imap_in, |
| 89 | + 'pop3_imap_out' => (int)$day->pop3_imap_out |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + $result[] = [ |
| 94 | + 'id' => (string)$webspace->id, |
| 95 | + 'status' => (string)$webspace->status, |
| 96 | + 'error_code' => (int)$webspace->errcode, |
| 97 | + 'error_text' => (string)$webspace->errtext, |
| 98 | + 'traffic' => $traffic, |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + return $result; |
| 103 | + } |
| 104 | +} |
0 commit comments