<commit_before><?php namespace Orchestra\Foundation\Processor;

use Exception;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Orchestra\Foundation\Presenter\Account as AccountPresenter;
use Orchestra\Foundation\Validation\Account as AccountValidator;
use Orchestra\Model\User;
use Orchestra\Notifier\Message;
use Orchestra\Support\Facades\App;
use Orchestra\Support\Facades\Notifier;
use Orchestra\Support\Str;

class Registration extends AbstractableProcessor
{
    /**
     * Create a new processor instance.
     *
     * @param  \Orchestra\Foundation\Presenter\Account  $presenter
     * @param  \Orchestra\Foundation\Validation\Account $validator
     */
    public function __construct(AccountPresenter $presenter, AccountValidator $validator)
    {
        $this->presenter = $presenter;
        $this->validator = $validator;
    }

    /**
     * View registration page.
     *
     * @param  object  $listener
     * @return mixed
     */
    public function index($listener)
    {
        $eloquent = App::make('orchestra.user');

        $title = 'orchestra/foundation::title.register';
        $form  = $this->presenter->profile($eloquent, 'orchestra::register');

        $form->extend(function ($form) use ($title) {
            $form->submit = $title;
        });

        Event::fire('orchestra.form: user.account', array($eloquent, $form));

        return $listener->indexSucceed(compact('eloquent', 'form'));
    }

    /**
     * Create a new user.
     *
     * @param  object  $listener
     * @param  array   $input
     * @return mixed
     */
    public function create($listener, array $input)
    {
        $password = Str::random(5);

        $validation = $this->validator->on('register')->with($input);

        // Validate user registration, if any errors is found redirect it
        // back to registration page with the errors
        if ($validation->fails()) {
            return $listener->createValidationFailed($validation);
        }

        $user = App::make('orchestra.user');

        $user->email    = $input['email'];
        $user->fullname = $input['fullname'];
        $user->password = $password;

        try {
            $this->fireEvent('creating', array($user));
            $this->fireEvent('saving', array($user));

            DB::transaction(function () use ($user) {
                $user->save();
                $user->roles()->sync(array(
                    Config::get('orchestra/foundation::roles.member', 2)
                ));
            });

            $this->fireEvent('creating', array($user));
            $this->fireEvent('saving', array($user));
        } catch (Exception $e) {
            return $listener->createFailed(array('error' => $e->getMessage()));
        }

        return $this->sendEmail($listener, $user, $password);
    }

    /**
     * Send new registration e-mail to user.
     *
     * @param  object                  $listener
     * @param  \Orchestra\Model\User   $user
     * @param  string                  $password
     * @return mixed
     */
    protected function sendEmail($listener, User $user, $password)
    {
        // Converting the user to an object allow the data to be a generic
        // object. This allow the data to be transferred to JSON if the
        // mail is send using queue.<commit_msg>Fixed event names on registration<commit_after><?php namespace Orchestra\Foundation\Processor;

use Exception;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Orchestra\Foundation\Presenter\Account as AccountPresenter;
use Orchestra\Foundation\Validation\Account as AccountValidator;
use Orchestra\Model\User;
use Orchestra\Notifier\Message;
use Orchestra\Support\Facades\App;
use Orchestra\Support\Facades\Notifier;
use Orchestra\Support\Str;

class Registration extends AbstractableProcessor
{
    /**
     * Create a new processor instance.
     *
     * @param  \Orchestra\Foundation\Presenter\Account  $presenter
     * @param  \Orchestra\Foundation\Validation\Account $validator
     */
    public function __construct(AccountPresenter $presenter, AccountValidator $validator)
    {
        $this->presenter = $presenter;
        $this->validator = $validator;
    }

    /**
     * View registration page.
     *
     * @param  object  $listener
     * @return mixed
     */
    public function index($listener)
    {
        $eloquent = App::make('orchestra.user');

        $title = 'orchestra/foundation::title.register';
        $form  = $this->presenter->profile($eloquent, 'orchestra::register');

        $form->extend(function ($form) use ($title) {
            $form->submit = $title;
        });

        Event::fire('orchestra.form: user.account', array($eloquent, $form));

        return $listener->indexSucceed(compact('eloquent', 'form'));
    }

    /**
     * Create a new user.
     *
     * @param  object  $listener
     * @param  array   $input
     * @return mixed
     */
    public function create($listener, array $input)
    {
        $password = Str::random(5);

        $validation = $this->validator->on('register')->with($input);

        // Validate user registration, if any errors is found redirect it
        // back to registration page with the errors
        if ($validation->fails()) {
            return $listener->createValidationFailed($validation);
        }

        $user = App::make('orchestra.user');

        $user->email    = $input['email'];
        $user->fullname = $input['fullname'];
        $user->password = $password;

        try {
            $this->fireEvent('creating', array($user));
            $this->fireEvent('saving', array($user));

            DB::transaction(function () use ($user) {
                $user->save();
                $user->roles()->sync(array(
                    Config::get('orchestra/foundation::roles.member', 2)
                ));
            });

            $this->fireEvent('created', array($user));
            $this->fireEvent('saved', array($user));
        } catch (Exception $e) {
            return $listener->createFailed(array('error' => $e->getMessage()));
        }

        return $this->sendEmail($listener, $user, $password);
    }

    /**
     * Send new registration e-mail to user.
     *
     * @param  object                  $listener
     * @param  \Orchestra\Model\User   $user
     * @param  string                  $password
     * @return mixed
     */
    protected function sendEmail($listener, User $user, $password)
    {
        // Converting the user to an object allow the data to be a generic
        // object. This allow the data to be transferred to JSON if the
        // mail is send using queue.
{'fraction_non_alphanumeric': 0.1337394836089353, 'fraction_numerical': 0.0005802146794313896, 'mean_word_length': 2.404938271604938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4811700', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1876, 'n_words': 435}