<?php
namespace NethServer\Module\Profile;

/*
 * Copyright (C) 2018 LibreSchool Project
 * 
 * This script is part of the LibreSchool Project.
 * 
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LibreSchool stuff.  If not, see <http://www.gnu.org/licenses/>.
 */

use Nethgui\System\PlatformInterface as Validate;

/**
 * Manages profile 
 *
 * Fires events
 * - profile-clean
 * - profile-gui-clean
 * - profile-lo-clean
 */
class Clean extends \Nethgui\Controller\Table\AbstractAction
{

    public function __construct($identifier = NULL)
    {
        if ($identifier !== 'all' && $identifier !== 'gui' && $identifier !== 'libreoffice') {
            throw new \InvalidArgumentException(sprintf('%s: module identifier must be one of "all", "gui" or "libreoffice".', get_class($this)), 1325579395);
        }
        parent::__construct($identifier);
    }

    public function bind(\Nethgui\Controller\RequestInterface $request)
    {
        $this->declareParameter('username', Validate::USERNAME);

        parent::bind($request);
        $username = \Nethgui\array_end($request->getPath());

        if ( ! $username) {
            throw new \Nethgui\Exception\HttpException('Not found', 404, 1322148400);
        }

        $this->parameters['username'] = $username;
    }

    public function process()
    {
        if ( ! $this->getRequest()->isMutation()) {
            return;
        }

	$event = "libreoffice"; // it is the less impacting.
	switch ($this->getIdentifier()) {
	    case "all":
		$event = "profile-clean";
		break;
	    case "gui":
		$event = "profile-gui-clean";
		break;
	    case "libreoffice":
		$event = "profile-lo-clean";
		break;
	}

        $this->getPlatform()->signalEvent(sprintf("%s@post", $event), array($this->parameters['username']));
    }

}