F
F
Flaker2014-06-19 11:43:11
Yii
Flaker, 2014-06-19 11:43:11

(Yii2) What's the best way to deal with user avatars?

1) Should the methods for working with the avatar be separated from the User Model ?
2) Where should the methods be located?
3) How can the User Model access them, and should it even have access to them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Makarov, 2014-06-19
@Flaker

1) Desirable.
2) In a separate class.
3) You can do it in different ways. For convenience, I would do something like this:

class AvatarManager
{
}

class User
{
    private $avatarManager;

    protected function getAvatarManager()
    {
        // здесь можно устроить и честный dependency injection, если планируется писать
        // честные модульные тесты или есть подозрения, что менеджеров будет не один
        return $this->avatarManager === null ? new AvatarManager($this) : $this->avatarManager;
    }

    public function getAvatarUrl($width, $height)
    {
         return $this->getAvatarManager()->getUrl($width, $height);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question