-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate unique avatar on contact creation (monicahq/chandler#64)
- Loading branch information
Showing
15 changed files
with
980 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use App\Models\Avatar; | ||
use App\Models\Contact; | ||
use App\Models\MultiAvatar; | ||
use Faker\Factory as Faker; | ||
|
||
class AvatarHelper | ||
{ | ||
/** | ||
* Generate a new random avatar. | ||
* | ||
* The Multiavatar library takes a name to generate a unique avatar. | ||
* However, contacts can be created in Monica without a name. When this case | ||
* happens, we'll generate a fake name for the contact, and generate an avatar | ||
* based on that name. | ||
* | ||
* @param Contact $contact | ||
* @return Avatar | ||
*/ | ||
public static function generateRandomAvatar(Contact $contact): Avatar | ||
{ | ||
$multiavatar = new MultiAvatar(); | ||
|
||
if (is_null($contact->first_name)) { | ||
$name = Faker::create()->name(); | ||
} else { | ||
$name = $contact->first_name.' '.$contact->last_name; | ||
} | ||
|
||
$svgCode = $multiavatar($name, null, null); | ||
|
||
$avatar = Avatar::create([ | ||
'contact_id' => $contact->id, | ||
'type' => Avatar::TYPE_GENERATED, | ||
'svg' => $svgCode, | ||
]); | ||
|
||
return $avatar; | ||
} | ||
|
||
/** | ||
* Get the avatar of a contact. | ||
* | ||
* @param Contact $contact | ||
* @return string | ||
*/ | ||
public static function getSVG(Contact $contact): string | ||
{ | ||
$avatar = $contact->avatar; | ||
|
||
return $avatar->svg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class Avatar extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'avatars'; | ||
|
||
/** | ||
* Possible type. | ||
*/ | ||
const TYPE_GENERATED = 'generated'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'contact_id', | ||
'type', | ||
'svg', | ||
]; | ||
|
||
/** | ||
* Get the contact associated with the contact log. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function contact() | ||
{ | ||
return $this->belongsTo(Contact::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.