<?php

namespace app\models\mikrotik\ip\trafficflow;

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 Trafficflow extends Model {

    public $enabled;
    public $interfaces;
    public $cache_entries;
    public $active_flow_timeout;
    public $inactive_flow_timeout;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['cache_entries', 'string'],
            ['active_flow_timeout', 'string'],
            ['inactive_flow_timeout', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/traffic-flow/getall');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->interfaces = $response->getProperty('interfaces');
        $this->cache_entries = $response->getProperty('cache-entries');
        $this->active_flow_timeout = $response->getProperty('active-flow-timeout');
        $this->inactive_flow_timeout = $response->getProperty('inactive-flow-timeout');
        return $this;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/traffic-flow/set');
        $setRequest->setargument('.id', $id);
        if ($this->enabled != "") {
            $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        }
        if ($this->interfaces != "") {
            $setRequest->setargument('interfaces', $this->interfaces);
        }
        if ($this->cache_entries != "") {
            $setRequest->setargument('cache-entries', $this->cache_entries);
        }
        if ($this->active_flow_timeout != "") {
            $setRequest->setargument('active-flow-timeout', $this->active_flow_timeout);
        }
        if ($this->inactive_flow_timeout != "") {
            $setRequest->setargument('inactive-flow-timeout', $this->inactive_flow_timeout);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

}
