<?php

namespace Buckhill\Net;

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)
        {
            $mac = exec(sprintf($cmd, escapeshellarg($interface)), $output, $result);

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

            $macs[] = $mac[0];
        }

        return $macs;
    }
}