<?php
/**
 * Created by PhpStorm.
 * User: robin
 * Date: 11/24/15
 * Time: 5:17 PM
 */

namespace App\Http\Business\Commands;


class WakeOnLanCommand extends AbstractCommand
{

    /**
     * @return bool
     */
    public function exec()
    {
        $data = $this->_command->getData();
        $args = [
            'wakeonlan',
            '-i',
            escapeshellarg($data['IP']),
            '-p',
            escapeshellarg($data['Port'])
        ];
        foreach ($data["MACs"] as $mac) {
            $args[] = escapeshellarg($mac);
        }
        $cli = implode(' ', $args);
        $code = 0;
        $output = [];
        exec($cli, $output, $code);
        if ($code != 0) {
            abort(500, "Wakeonlan failed with code " . $code);
        }
    }
}