New WS\Message types added: post_edited, comment_edited. Validation slightly refactored.

This commit is contained in:
Alexey Skobkin 2019-02-25 20:02:26 +03:00
parent 4d7549bddd
commit b1d047941c

View file

@ -11,9 +11,11 @@ use Skobkin\Bundle\PointToolsBundle\Exception\WebSocket\UnsupportedTypeException
class Message implements ValidableInterface class Message implements ValidableInterface
{ {
public const TYPE_COMMENT = 'comment'; public const TYPE_COMMENT = 'comment';
public const TYPE_COMMENT_EDITED = 'comment_edited';
public const TYPE_POST = 'post'; public const TYPE_POST = 'post';
public const TYPE_POST_EDITED = 'post_edited';
public const TYPE_POST_COMMENT_RECOMMENDATION = 'rec'; public const TYPE_POST_COMMENT_RECOMMENDATION = 'rec';
public const TYPE_COMMENT_RECOMMENDATION = 'ok'; public const TYPE_RECOMMENDATION_WITH_COMMENT = 'ok';
/** /**
* Event type. @see Message::TYPE_* constants * Event type. @see Message::TYPE_* constants
@ -136,7 +138,7 @@ class Message implements ValidableInterface
public function isCommentRecommendation(): bool public function isCommentRecommendation(): bool
{ {
return self::TYPE_COMMENT_RECOMMENDATION === $this->a; return self::TYPE_RECOMMENDATION_WITH_COMMENT === $this->a;
} }
public function isPostRecommendation(): bool public function isPostRecommendation(): bool
@ -152,50 +154,27 @@ class Message implements ValidableInterface
{ {
switch ($this->a) { switch ($this->a) {
case self::TYPE_POST: case self::TYPE_POST:
if ( return $this->isValidPost();
null !== $this->author && break;
null !== $this->authorId &&
null !== $this->html && case self::TYPE_POST_EDITED:
null !== $this->postId && return $this->isValidPostEdited();
null !== $this->private &&
null !== $this->tags
) {
return true;
}
break; break;
case self::TYPE_COMMENT; case self::TYPE_COMMENT;
if ( return $this->isValidComment();
null !== $this->author &&
null !== $this->authorId &&
null !== $this->commentId &&
null !== $this->html &&
null !== $this->postId &&
null !== $this->text
) {
return true;
}
break; break;
case self::TYPE_COMMENT_RECOMMENDATION; case self::TYPE_COMMENT_EDITED;
if ( return $this->isValidCommentEdited();
null !== $this->author && break;
null !== $this->authorId &&
null !== $this->postId case self::TYPE_RECOMMENDATION_WITH_COMMENT;
) { return $this->isValidRecommendationWithComment();
return true;
}
break; break;
case self::TYPE_POST_COMMENT_RECOMMENDATION; case self::TYPE_POST_COMMENT_RECOMMENDATION;
if ( return $this->isValidPostCommentRecommendation();
null !== $this->author &&
null !== $this->authorId &&
null !== $this->postId &&
null !== $this->postAuthorId
) {
return true;
}
break; break;
case null: case null:
@ -205,8 +184,6 @@ class Message implements ValidableInterface
default: default:
throw new UnsupportedTypeException(sprintf('Type \'%s\' is not supported.', $this->a)); throw new UnsupportedTypeException(sprintf('Type \'%s\' is not supported.', $this->a));
} }
return false;
} }
public function getA(): string public function getA(): string
@ -303,4 +280,52 @@ class Message implements ValidableInterface
{ {
return $this->toUsers; return $this->toUsers;
} }
private function hasCommonMandatoryData(): bool
{
return (
null !== $this->author &&
null !== $this->authorId &&
null !== $this->postId
);
}
private function isValidPost(): bool
{
return $this->hasCommonMandatoryData() && (
// Text can be empty ("") though
null !== $this->text &&
null !== $this->private &&
null !== $this->tags
);
}
private function isValidPostEdited(): bool
{
return $this->isValidPost();
}
private function isValidComment(): bool
{
return $this->hasCommonMandatoryData() && (
null !== $this->commentId &&
null !== $this->html &&
null !== $this->text
);
}
private function isValidCommentEdited(): bool
{
return $this->hasCommonMandatoryData() && (null !== $this->commentId);
}
private function isValidRecommendationWithComment(): bool
{
return $this->hasCommonMandatoryData();
}
private function isValidPostCommentRecommendation(): bool
{
return $this->hasCommonMandatoryData() && (null !== $this->postAuthorId);
}
} }