<?php

namespace app\models\mikrotik\ip\socks;

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

    public $enabled;
    public $port;
    public $connection_idle_timeout;
    public $max_connections;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['port', 'integer'],
            ['connection_idle_timeout', 'string'],
            ['max_connections', 'integer'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/socks/getall');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->port = $response->getProperty('port');
        $this->connection_idle_timeout = $response->getProperty('connection-idle-timeout');
        $this->max_connections = $response->getProperty('max-connections');


        return $this;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/socks/set');
        if ($this->enabled != "") {
            $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        }
        if ($this->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->connection_idle_timeout != "") {
            $setRequest->setargument('connection-idle-timeout', $this->connection_idle_timeout);
        }
        if ($this->max_connections != "") {
            $setRequest->setargument('max-connections', $this->max_connections);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

}
