journal-intime/app/User.php

85 lines
2.0 KiB
PHP

<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Log;
use Spatie\Image\Exceptions\InvalidManipulation;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
class User extends Authenticatable implements HasMedia
{
use Notifiable, HasMediaTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'notification_hour', 'encrypt_messages',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $dates = [
'created_at', 'updated_at', 'email_verified_at',
];
/**
* @return string
*/
public function getFolder()
{
$arrayHash = str_split(strtolower(md5($this->id)));
return sprintf('%s/%s', $arrayHash[0], $arrayHash[1]);
}
/**
* @return bool
*/
public function isE2E()
{
return (bool) $this->encrypt_messages;
}
/**
* @param Media|null $media
*/
public function registerMediaConversions(Media $media = null)
{
try {
$this->addMediaConversion('thumb')
->width(100)
->height(100)
->optimize();
$this->addMediaConversion('icon')
->width(32)
->height(32)
->optimize();
} catch (InvalidManipulation $e) {
Log::alert(sprintf('Error while manipulating Avatar for %s (%s)', $this->email, $e->getMessage()));
}
}
/**
* @param string $alias
* @return string
*/
public function getAvatar($alias = 'thumb'): string
{
return $this->getFirstMediaUrl('avatars', $alias);
}
}