<?php

namespace app\models\mikrotik\ip\ipsec;

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

    public $id;
    public $local_address;
    public $remote_address;

    public function rules() {
        return [
            ['id', 'string'],
            ['local_address', 'string'],
            ['remote_address', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/remote-peers/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Remote();
                $c->id = $response->getProperty('.id');
                $c->local_address = $response->getProperty('local_address');
                $c->remote_address = $response->getProperty('remote_address');
                $return[] = $c;
            }
        }
        return $return;
    }

}
