<?php
/**
 * Created by PhpStorm.
 * User: eater
 * Date: 4/2/16
 * Time: 5:37 PM
 */

namespace Eater\Glim\Handler\Register;


use Eater\Glim\Handler\Main;
use Eater\Glim\Service\User;

class Action extends Main
{
    public function handle()
    {
        $invite   = $this->post('invite');
        $username = $this->post('username');
        $password = $this->post('password');

        /** @var User $user */
        $user = $this->get('user');

        /* @var \Aura\Session\Session $session */
        $session = $this->get('session');
        $segment = $session->getSegment('main');

        $newUser = null;

        try {
            $newUser = $user->register($invite, $username, $password);
        } catch (\Exception $e) {
            $segment->setFlash("error", $e->getMessage());
        }

        if ($newUser === null) {
            return $this->redirect('/register');
        }

        $segment->set('user', $newUser);

        return $this->redirect('/panel');
    }
}