<?php

namespace app\models\mikrotik\ip\webproxy;

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 $dst_address;
    public $protocol;
    public $rx_bytes;
    public $src_address;
    public $state;
    public $tx_bytes;

    public function rules() {
        return [
            ['dst_address', 'string'],
            ['protocol', 'string'],
            ['rx_bytes', 'string'],
            ['src_address', 'string'],
            ['state', 'string'],
            ['tx_bytes', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/proxy/connections/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->dst_address = $response->getProperty('dst-address');
                $c->protocol = $response->getProperty('protocol');
                $c->rx_bytes = $response->getProperty('rx-bytes');
                $c->src_address = $response->getProperty('src-address');
                $c->state = $response->getProperty('state');
                $c->tx_bytes = $response->getProperty('tx-bytes');
                $return[] = $c;
            }
        }
        return $return;
    }

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

}
