<?php
/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */

namespace Foliolabs\Docman\Site;

use Foliolabs\Library;
use Foliolabs\WP;

/**
 * Error event subscriber
 *
 * @author  Arunas Mazeika <https://github.com/amazeika>
 * @package Foliolabs\Component\Base
 */
class EventSubscriberNotfound extends Library\EventSubscriberAbstract
{    
    protected function _initialize(Library\ObjectConfig $config)
    {
        $config->append(array(
            'priority' => Library\Event::PRIORITY_HIGH
        ));

        parent::_initialize($config);
    }

    public function onException(Library\EventInterface $event)
    {
        $user = $this->getObject('user');

        $exception = $event->exception;

        if (!$user->isAuthentic() && $exception instanceof \Foliolabs\Docman\ControllerExceptionUnauthorizedCategory)
        {
            $request = $this->getObject('request');

            // Change the event context (exception) as to force a login re-direct
            if (!WP::is_admin() && $request->getFormat() == 'html' && $request->isSafe()) {
                $event->exception = new Library\HttpExceptionUnauthorized("", 0, $exception);
            }
        }
    }
}