Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel, Doctrine and the use of Value Objects to model Authentication Data #387

Open
marcosdipaolo opened this issue Jul 7, 2019 · 1 comment

Comments

@marcosdipaolo
Copy link

marcosdipaolo commented Jul 7, 2019

Hi there. I've been trying lately the use of Doctrine along with Laravel with LaravelDoctrine.

I was happy when i found out that actually you can make the laravel auth system work with Doctrine, its Entities and all, using some of its traits with the help of the docs and this helpful article.

All good, everything works. The only thing I wanted is a Laravel Doctrine bolierplate repo to save time every time I need to start a new prioject.

But..., here's the deal. As I code i feel the need of modeling better the name, email and password, those are the users table fields, table which was set up using Fluent .
What do I mean by modeling?, the use of Value objects. With them i can take care of certain specific validation if I want, hashing if i want to do it myself, etc etc.

So my UserMapping.php file would look like this:

<?php

namespace App\Infrastructure\Mappings;

use LaravelDoctrine\Fluent\EntityMapping;
use LaravelDoctrine\Fluent\Fluent;
use Src\Entities\User;
use Src\ValueObjects\Email;
use Src\ValueObjects\Name;
use Src\ValueObjects\Password;

class UserMapping extends EntityMapping
{
    public function mapFor()
    {
        return User::class;
    }

    public function map(Fluent $builder)
    {
        $builder->bigIncrements('id');
        $builder->embed(Name::class)->noPrefix();
        $builder->embeed(Password::class)->noPrefix();
        $builder->embeed(Email::class)->noPrefix();
        $builder->timestamps();
    }
}

Point is, is this posible? Is laravel auth system flexible enough? is it a good idea? (it sounds good to me). Is it a lot of work?. Question arose when yesterday i tried really quickly and didn't work out of the box. I'll be very glad if i get some opinions about this.

@eigan
Copy link
Member

eigan commented Jul 14, 2019

The Laravel auth system requires you to implement the Illuminate\Contracts\Auth\Authenticatable. If you return proper strings instead of the value objects (or implement __toString() on the VO), then it might work I guess.

I am not into the Fluent stuff, but why cant you just validate in the setter?

class User
{
    /**
     * @var string
     */
    private $name;

    public function setName(string $name): void
    {
        // Validate $name
        if(empty($name)) {
            throw new InvalidArgumentException("Name cannot be empty");
        }

        $this->name = $name;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants