Skip to content

Commit

Permalink
update thread creator to return null user model if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr committed Oct 4, 2017
1 parent 4bb6ca9 commit 0a1d2f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class Thread extends Eloquent
*/
protected $dates = ['deleted_at'];

/**
* Creator of Thread
*
* @User null
*/
protected $creator = null;
/**
* Internal cache for creator.
*
* @var null|Models::user()
*/
protected $creatorCache = null;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -99,15 +99,16 @@ public function users()
/**
* Returns the user object that created the thread.
*
* @return mixed
* @return Models::user()
*/
public function creator()
{
if (is_null($this->creator)) {
if (is_null($this->creatorCache)) {
$firstMessage = $this->messages()->withTrashed()->oldest()->first();
$this->creator = $firstMessage ? $firstMessage->user : null;
$this->creatorCache = $firstMessage ? $firstMessage->user : Models::user();
}
return $this->creator;

return $this->creatorCache;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/EloquentThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ public function it_should_get_the_null_creator_of_a_thread_without_messages()

$thread->participants()->saveMany([$user_1, $user_2, $user_3]);

$this->assertNull($thread->creator());
$this->assertFalse($thread->creator()->exists);
$this->assertNull($thread->creator()->name);
}
}

0 comments on commit 0a1d2f7

Please sign in to comment.