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

use Exception,
	Event,
	Validator,
	Orchestra\Model\User,
	Orchestra\Model\Role;

class Installer {
	
	/**
	 * Application instance.
	 *
	 * @var Illuminate\Foundation\Application
	 */
	protected $app = null;

	/**
	 * Construct a new instance.
	 *
	 * @access public
	 * @return void
	 */
	public function __construct($app)
	{
		$this->app = $app;
		$file = rtrim($this->app['path'], '/').'/orchestra/installer.php';

		if ($this->app['files']->exists($file)) 
		{
			$this->app['files']->requireOnce($file);
		}
	}

	/**
	 * Migrate Orchestra Platform schema.
	 *
	 * @access public
	 * @return void
	 */
	public function migrate()
	{
		$this->app->make('orchestra.publisher.migrate')->foundation();
	}

	/**
	 * Create adminstrator account.
	 *
	 * @access public	
	 * @param  array    $input
	 * @return void
	 */
	public function createAdmin($input, $multipleAdmin = true) 
	{
		// Grab input fields and define the rules for user validations.
		$rules = array(
			'email'     => array('required', 'email'),
			'password'  => array('<PASSWORD>'),
			'fullname'  => array('required'),
			'site_name' => array('required'),
		);

		$validation = $this->app['validator']->make($input, $rules);

		// Validate user registration, we should stop this process if
		// the user not properly formatted.
		if ($validation->fails())
		{
			$this->app['session']->flash('errors', $validation->messages());
			return false;
		}

		try
		{
			! $multipleAdmin and $this->checkExistingUser();

			$this->runApplicationSetup($input);

			// Installation is successful, we should be able to generate
			// success message to notify the user. Installer route will be
			// disabled after this point.
			$this->app['orchestra.messages']->add('success', trans('orchestra/foundation::install.user.created'));

			return true;
		}
		catch (Exception $e)
		{
			$this->app['orchestra.messages']->add('error', $e->getMessage());

			return false;
		}
	}

	/**
	 * Run application setup
	 *
	 * @access protected
	 * @param  array    $input
	 * @return void
	 */
	protected function runApplicationSetup($input)
	{
		$user    = $this->createUser($input);
		$actions = array('Manage Orchestra', 'Manage Users');
		$admin   = $this->app['config']->get('orchestra/auth::role.admin');
		$roles   = Role::lists('name', 'id');

		// Attach Administrator role to the newly created administrator.
		$user->roles()->sync(array($admin));

		$memory = $this->app['orchestra.memory']->make();
		$memory->put('site.name', $input['site_name']);
		$memory->put('site.theme.backend', 'default');
		$memory->put('site.theme.frontend', 'default');
		$memory->put('email', $this->app['config']->get('mail'));
		$memory->put('email.from', array(
			'name'    => $input['site_name'],
			'address' => $input['email']
		);

		// We should also create a basic ACL for Orchestra.
		$acl = $this->app['orchestra.acl']->make('orchestra');
		$acl->actions()->fill($actions);
		$acl->roles()->fill(array_values($roles));
		$acl->allow($roles[$admin], $actions);

		$this->app['events']->fire('orchestra.install: acl', array($acl));

		$acl->attach($memory);
	}

	/**
	 * Create user account.
	 *
	 * @access protected
	 * @param  array    $input
	 * @return Orchestra\Model\User
	 */
	protected function createUser($input)
	{
		$user           = new User;
		$user->email    = $input['email'];
		$user->password = $input['password'];
		$user->fullname = $input['fullname'];
		$user->status   = 0;

		$this->app['events']->fire('orchestra.install: user', array($user, $input));

		$user->save();

		return $user;
	}

	/**
	 * Check for existing User.
	 *
	 * @access protected
	 * @return boolean
	 */
	protected function checkExistingUser()
	{
		$users = User::all();

		// Before we create administrator, we should ensure that users table 
		// is empty to avoid any possible hijack or invalid request.
		if ( ! empty($users))
		{
			throw new Exception(trans('orchestra/foundation::install.user.duplicate'));
		}
	}
}<commit_msg>Refactor installation code.<commit_after><?php namespace Orchestra\Foundation\Installation;

use Exception,
	Event,
	Validator,
	Orchestra\Model\User,
	Orchestra\Model\Role;

class Installer {
	
	/**
	 * Application instance.
	 *
	 * @var Illuminate\Foundation\Application
	 */
	protected $app = null;

	/**
	 * Construct a new instance.
	 *
	 * @access public
	 * @return void
	 */
	public function __construct($app)
	{
		$this->app = $app;
		$file = rtrim($this->app['path'], '/').'/orchestra/installer.php';

		if ($this->app['files']->exists($file)) 
		{
			$this->app['files']->requireOnce($file);
		}
	}

	/**
	 * Migrate Orchestra Platform schema.
	 *
	 * @access public
	 * @return void
	 */
	public function migrate()
	{
		$this->app->make('orchestra.publisher.migrate')->foundation();
	}

	/**
	 * Create adminstrator account.
	 *
	 * @access public	
	 * @param  array    $input
	 * @return void
	 */
	public function createAdmin($input, $multipleAdmin = true) 
	{
		// Grab input fields and define the rules for user validations.
		$rules = array(
			'email'     => array('required', 'email'),
			'password'  => array('<PASSWORD>'),
			'fullname'  => array('required'),
			'site_name' => array('required'),
		);

		$validation = $this->app['validator']->make($input, $rules);

		// Validate user registration, we should stop this process if
		// the user not properly formatted.
		if ($validation->fails())
		{
			$this->app['session']->flash('errors', $validation->messages());
			return false;
		}

		try
		{
			! $multipleAdmin and $this->checkExistingUser();

			$this->runApplicationSetup($input);

			// Installation is successful, we should be able to generate
			// success message to notify the user. Installer route will be
			// disabled after this point.
			$this->app['orchestra.messages']->add('success', trans('orchestra/foundation::install.user.created'));

			return true;
		}
		catch (Exception $e)
		{
			$this->app['orchestra.messages']->add('error', $e->getMessage());

			return false;
		}
	}

	/**
	 * Run application setup
	 *
	 * @access protected
	 * @param  array    $input
	 * @return void
	 */
	protected function runApplicationSetup($input)
	{
		// Bootstrap auth services, so we can use orchestra/auth package 
		// configuration.
		$user    = $this->createUser($input);
		$memory  = $this->app['orchestra.memory']->make();
		$actions = array('Manage Orchestra', 'Manage Users');
		$admin   = $this->app['config']->get('orchestra/auth::role.admin', 1);
		$roles   = Role::lists('name', 'id');
		$site    = array(
			'name'  => $input['site_name'],
			'theme' => array(
				'frontend' => 'default',
				'backend'  => 'default',
			),
		);

		// Attach Administrator role to the newly created administrator.
		$user->roles()->sync(array($admin));

		// Add some basic configuration for Orchestra Platform, including 
		// email configuration.
		$memory->put('site', $site);
		$memory->put('email', $this->app['config']->get('mail'));
		$memory->put('email.from', array(
			'name'    => $input['site_name'],
			'address' => $input['email']
		));

		// We should also create a basic ACL for Orchestra Platform, since 
		// the basic roles is create using Fluent Query Builder we need 
		// to manually insert the roles.
		$acl = $this->app['orchestra.acl']->make('orchestra');
		$acl->actions()->fill($actions);
		$acl->roles()->fill(array_values($roles));
		$acl->allow($roles[$admin], $actions);

		$this->app['events']->fire('orchestra.install: acl', array($acl));

		$acl->attach($memory);
	}

	/**
	 * Create user account.
	 *
	 * @access protected
	 * @param  array    $input
	 * @return Orchestra\Model\User
	 */
	protected function createUser($input)
	{
		$user           = new User;
		$user->email    = $input['email'];
		$user->password = $input['password'];
		$user->fullname = $input['fullname'];
		$user->status   = 0;

		$this->app['events']->fire('orchestra.install: user', array($user, $input));

		$user->save();

		return $user;
	}

	/**
	 * Check for existing User.
	 *
	 * @access protected
	 * @return boolean
	 */
	protected function checkExistingUser()
	{
		$users = User::all();

		// Before we create administrator, we should ensure that users table 
		// is empty to avoid any possible hijack or invalid request.
		if ( ! empty($users))
		{
			throw new Exception(trans('orchestra/foundation::install.user.duplicate'));
		}
	}
}
{'fraction_non_alphanumeric': 0.1832404883252341, 'fraction_numerical': 0.0003555766267630674, 'mean_word_length': 3.5487870619946094, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 125, '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': '4549820', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2653, 'n_words': 617}