1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 
<?php
namespace Omeka\Installation\Task;

use Omeka\Api\Exception\ValidationException;
use Omeka\Installation\Installer;

/**
 * Create the first user.
 */
class CreateFirstUserTask implements TaskInterface
{
    public function perform(Installer $installer)
    {
        $apiManager = $installer->getServiceLocator()->get('Omeka\ApiManager');
        $entityManager = $installer->getServiceLocator()->get('Omeka\EntityManager');

        $vars = $installer->getVars('Omeka\Installation\Task\CreateFirstUserTask');
        try {
            $response = $apiManager->create('users', [
                'o:is_active' => true,
                'o:role' => 'global_admin',
                'o:name' => $vars['name'],
                'o:email' => $vars['email'],
            ]);
        } catch (ValidationException $e) {
            $installer->addErrorStore($e->getErrorStore());
            return;
        }

        // Set the password.
        $user = $response->getContent()->jsonSerialize();
        $userEntity = $entityManager->find('Omeka\Entity\User', $user['o:id']);
        $userEntity->setPassword($vars['password']);
        $entityManager->flush();
    }
}