Skip to content

Commit

Permalink
Splits comment model into ArticleDiscussion and ArticleComment
Browse files Browse the repository at this point in the history
  • Loading branch information
artstorm committed Aug 25, 2015
1 parent c193798 commit 68bf5e3
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 228 deletions.
10 changes: 5 additions & 5 deletions api/app/Models/Article.php
Expand Up @@ -90,15 +90,15 @@ protected static function boot()
});

static::created(function (Article $article) {
$articleComment = (new ArticleComment)->setArticle($article);
$articleComment->newDiscussion();
$articleDiscussion = (new ArticleDiscussion)->setArticle($article);
$articleDiscussion->createDiscussion();

return true;
});

static::deleted(function (Article $article) {
$articleComment = (new ArticleComment)->setArticle($article);
$articleComment->deleteDiscussion();
$articleDiscussion = (new ArticleDiscussion)->setArticle($article);
$articleDiscussion->deleteDiscussion();

return true;
});
Expand Down Expand Up @@ -164,7 +164,7 @@ public function metas()
*/
public function comments()
{
return (new ArticleComment)->setArticle($this);
return (new ArticleDiscussion)->setArticle($this);
}

public function tags()
Expand Down
219 changes: 0 additions & 219 deletions api/app/Models/ArticleComment.php
Expand Up @@ -2,34 +2,10 @@

namespace App\Models;

use App;
use Spira\Model\Model\BaseModel;
use Spira\Model\Collection\Collection;
use App\Services\Api\Vanilla\Client as VanillaClient;

class ArticleComment extends BaseModel
{
/**
* Article discussion belongs to.
*
* @var Article
*/
protected $article;

/**
* Vanilla API client.
*
* @var VanillaClient
*/
protected $client;

/**
* Models to constrain with on parent collections.
*
* @var array
*/
protected $eagerConstraints = [];

/**
* The attributes that are mass assignable.
*
Expand All @@ -52,199 +28,4 @@ class ArticleComment extends BaseModel
protected $casts = [
'created_at' => 'datetime',
];

/**
* Create a discussion thread for the article.
*
* @return void
*/
public function newDiscussion()
{
$this->getClient()->api('discussions')->create(
$this->article->title,
$this->article->excerpt,
1,
['ForeignID' => $this->article->article_id]
);
}

/**
* Delete the discussion thread for the article.
*
* @return void
*/
public function deleteDiscussion()
{
$this->getClient()->api('discussions')->removeByForeignId(
$this->article->article_id
);
}

/**
* Get the collection of comments.
*
* @see \Illuminate\Database\Eloquent\Relations\HasMany::getResults()
*
* @return Collection
*/
public function getResults()
{
// First a minimal call to the discussion for the total comment count
$discussion = $this->getClient()->api('discussions')->findByForeignId(
$this->article->article_id,
1,
1
);

$commentCount = $discussion['Discussion']['CountComments'];

// Now get the entire batch of comments
$discussion = $this->getClient()->api('discussions')->findByForeignId(
$this->article->article_id,
1,
$commentCount
);

$comments = $this->prepareCommentsForHydrate($discussion['Comments']);
$comments = $this->hydrateRequestCollection($comments, new Collection);

return $comments;
}

/**
* Convert a comment from Vanilla to be ready to hydrate Eloquent model.
*
* @param array $comments
*
* @return array
*/
protected function prepareCommentsForHydrate(array $comments = [])
{
$map = [
'CommentID' => 'article_comment_id',
'Body' => 'body',
'DateInserted' => 'created_at',
'InsertName' => 'author_name',
'InsertEmail' => 'author_email',
'InsertPhoto' => 'author_photo'
];

$comments = array_map(function ($comment) use ($map) {
foreach ($comment as $key => $value) {
if (array_key_exists($key, $map)) {
$comment[$map[$key]] = $value;
}
}

return $comment;
}, $comments);

return $comments;
}

/**
* Set the constraints for an eager load of the relation.
*
* @see \Illuminate\Database\Eloquent\Relations\HasOneOrMany::addEagerConstraints()
*
* @param array $models
*
* @return void
*/
public function addEagerConstraints($models)
{
$this->eagerConstraints = $models;
}

/**
* Initialize the relation on a set of models.
*
* @see \Illuminate\Database\Eloquent\Relations\HasMany::initRelation()
*
* @param array $models
* @param string $relation
*
* @return array
*/
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, $this->newCollection());
}

return $models;
}

/**
* Get the relationship for eager loading.
*
* @see \Illuminate\Database\Eloquent\Relations\Relation::getEager()
*
* @return Collection
*/
public function getEager()
{
$results = new Collection;

foreach ($this->eagerConstraints as $model) {
$comment = new ArticleComment;
$comment->setArticle($model);
$results->offsetSet($model->getKey(), $comment->getResults());
}

return $results;
}

/**
* Match the eagerly loaded results to their parents.
*
* @see \Illuminate\Database\Eloquent\Relations\HasMany::match()
*
* @param array $models
* @param Collection $results
* @param string $relation
*
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
foreach ($models as $model) {
$key = $model->getKey();
if ($results->offsetExists($key)) {
$value = $results->offsetGet($key);

$model->setRelation($relation, $value);
}
}

return $models;
}

/**
* Sets the article the discussion belongs to.
*
* @param Article $article
*
* @return ArticleComment
*/
public function setArticle(Article $article)
{
$this->article = $article;

return $this;
}

/**
* Get Vanilla API client.
*
* @return VanillaClient
*/
protected function getClient()
{
if (!$this->client) {
$this->client = App::make(VanillaClient::class);
}

return $this->client;
}
}

0 comments on commit 68bf5e3

Please sign in to comment.