<?php

namespace Buckhill\Net;

use Buckhill\Net\Exceptions\NetworkException;

class MAC extends Network
{
    /**
     * Returns an array of MAC addresses for every interface found
     *
     * @return array
     * @throws NetworkException
     */
    public function getMAC()
    {
        $interfaces = $this->getInterfaces();

        if(empty($interfaces)) {
            throw new NetworkException("No network interfaces available");
        }

        $cmd =  "cat /sys/class/net/%s/address 2>&1";

        $macs = [];

        foreach($interfaces as $interface)
        {
            if ($interface !== 'bonding_masters') {
                exec(sprintf($cmd, escapeshellarg($interface)), $output, $result);

                if ($result !== 0) {
                    throw new NetworkException(sprintf("Unable to obtain MAC address for interface %s", $interface));
                }

                if (!in_array($output[0], $macs)) {
                    $macs[] = $output[0];
                }
            }
        }

        return $macs;
    }
}