<?php

namespace app\models\mikrotik\ip\firewall;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Connections extends Model {

    public $id;
    public $protocol;
    public $src_address;
    public $dst_address;
    public $timeout;
    public $orig_packets;
    public $orig_bytes;
    public $repl_packets;
    public $repl_bytes;

    public function rules() {
        return [
            ['id', 'string'],
            ['protocol', 'string'],
            ['src_address', 'string'],
            ['dst_address', 'string'],
            ['timeout', 'string'],
            ['orig_packets', 'string'],
            ['orig_bytes', 'string'],
            ['repl_packets', 'string'],
            ['repl_bytes', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/connection/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Connections();
                $c->id = $response->getProperty('.id');
                $c->protocol = $response->getProperty('protocol');
                $c->src_address = $response->getProperty('src-address');
                $c->dst_address = $response->getProperty('dst-address');
                $c->timeout = $response->getProperty('timeout');
                $c->orig_packets = $response->getProperty('orig-packets');
                $c->orig_bytes = $response->getProperty('orig-bytes');
                $c->repl_packets = $response->getProperty('repl-packets');
                $c->repl_bytes = $response->getProperty('repl-bytes');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip/firewall/connection');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
