public function up(EventEmitterInterface $globalEmitter) { $ps = $this->postService; $as = $this->attachmentService; $ps->getEventEmitter()->on(PostService::EVENT_CREATE, function (Post $post) use($as, $ps) { if ($post->hasAttachments()) { array_map(function (int $attachmentId) use($post, $as, $ps) { $as->attach(new PostAttachmentOwner($post->getId()), $as->getById($attachmentId)); }, $post->getAttachmentIds()); } }); $ps->getEventEmitter()->on(PostService::EVENT_DELETE, function (Post $post) use($as) { if ($post->hasAttachments()) { array_map(function (Attachment $attachment) use($as) { $as->destroy($attachment); }, $this->attachmentService->getManyByIds($post->getAttachmentIds())); } }); }
public function detectContentTypeOfPost(Post $post) : string { if (count($post->getAttachmentIds())) { $attachment = $this->attachmentService->getById($post->getAttachmentIds()[0]); switch ($resource = $attachment->getMetadata()['resource']) { default: throw new \Exception(sprintf('Unknown resource `%s`', $resource)); case YoutubeLinkMetadata::RESOURCE_TYPE: return ContentTypeCriteria::CONTENT_TYPE_VIDEO; case PageLinkMetadata::RESOURCE_TYPE: return ContentTypeCriteria::CONTENT_TYPE_LINK; case ImageLinkMetadata::RESOURCE_TYPE: return ContentTypeCriteria::CONTENT_TYPE_IMAGE; case WebmLinkMetadata::RESOURCE_TYPE: return ContentTypeCriteria::CONTENT_TYPE_VIDEO; case UnknownLinkMetadata::RESOURCE_TYPE: return ContentTypeCriteria::CONTENT_TYPE_TEXT; } } else { return ContentTypeCriteria::CONTENT_TYPE_TEXT; } }
private function formatAttachments(Post $post) : array { return array_map(function (Attachment $attachment) { return $this->attachmentFormatter->format($attachment); }, $this->attachmentService->getManyByIds($post->getAttachmentIds())); }