5
\$\begingroup\$

I've spent quite a bit of time studying the repository and entity patterns and this is what I came up with. I would appreciate it if you could post suggestions and critique.

User entity:

<?php
    namespace App\Entities\Entities\User;

    use
    App\Entities\Abstracts\AbstractEntity,
    App\Repositories\Interfaces\UserRepositoryInterface as UserRepository,
    App\Services\Validation\Laravel\UserLoginValidator,
    Str,
    Exception,
    ExceptionHandler,
    stdClass,
    MessageBag;

    class UserEntity extends AbstractEntity
    {
        protected $loginValidator;

        public function __construct(
            UserRepository $repository
            UserLoginValidator $loginValidator
            )
        {
            $this->repository = $repository;
            $this->loginValidator = $loginValidator;
        }


        public function login(array $input)
        {
            $result = $this->validate($this->loginValidator, $input);
            if(!$result instanceof MessageBag)
            {
                try
                {
                    $user = $this->repository->authenticate(array(
                        'email'     =>  $input['email'],
                        'password'  =>  $input['password']
                        ), false);
                    $this->save($user);
                    $user->type = $user->getGroups()[0]->id;

                    $result = new stdClass;
                    $result->errors = false;
                    $result->status_code = 23;
                    $result->user = $user->toArray();
                    return $result;
                }
                catch (Exception $e)
                {
                    return ExceptionHandler::returnException($e);
                }
            }
            else
                return $this->loginValidator->returnErrors();
        }

    }

User Repository:

<?php

    namespace App\Repositories\Eloquent\User;

    use
    App\Repositories\Abstracts\AbstractRepository,
    App\Repositories\Interfaces\UserRepositoryInterface,
    Cartalyst\Sentry\Sentry as Sentry,
    Cartalyst\Sentry\Users\Eloquent\User as SentryUser;

    class EloquentUserRepository extends AbstractRepository implements UserRepositoryInterface
    {
        public function __construct(Sentry $model)
        {
            $this->model = $model;
        }

        public function find($id)
        {
            return $this->model->findUserById($id);
        }

        public function save(SentryUser $model)
        {
            return $model->save();
        }

        public function getAccessToken()
        {
            return $this->model->getUser()->access_token;
        }
    }
?>

User controller:

<?php
    namespace Controllers\API;
    use
    BaseController,
    Input,
    ResponseSender,
    App\Entities\Entities\User\UserEntity;

    class UserController extends BaseController
    {
        protected $user;

        public function __construct(UserEntity $user)
        {
            $this->user = $user;
        }

        public function postLogin()
        {
            return ResponseSender::send($this->user->login(Input::all()));
        }

    }
?>
\$\endgroup\$
5
\$\begingroup\$

Maybe I'm just thinking of a different approach, but I normally would've reversed the dependency between UserEntity and UserRepository. I would expect the UserEntity to represent a singular record in the database and that I would use the UserRepository to retrieve one.

In this case, I would inject the UserRepository into the controller and call UserRepository.Login(userId, password) that would actually perform the lookup against the DB and return a UserEntity representing the logged in user.

Also, I would suggest defining interfaces for your injected classes so that it's easier to provide future implementations

\$\endgroup\$
1
\$\begingroup\$

I agree with @mklinker. You need to have separation of concerns.

Entities are object with an identity, therefore a user (single).

However, the job of the repository is to retrieve the data from persistent storage.

The login method should not be part of the user Entity class as this is an Authentication service which has nothing to do with the user as an entity.

\$\endgroup\$
  • \$\begingroup\$ PS: I do like your use of stdClass :) \$\endgroup\$ – Keith Mifsud Oct 11 '14 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.