attachmentHelper = $attachmentHelper; } /** * Determines what attachment should be used for previewing a part (especially in part table). * The returned attachment is guaranteed to be existing and be a picture. * @param Part $part The part for which the attachment should be determined * @return Attachment|null */ public function previewAttachment(Part $part) : ?Attachment { //First of all we check if the master attachment of the part is set (and a picture) $attachment = $part->getMasterPictureAttachment(); if ($this->isAttachmentValidPicture($attachment)) { return $attachment; } //Otherwise check if the part has a footprint with a valid masterattachment if ($part->getFootprint() !== null) { $attachment = $part->getFootprint()->getMasterPictureAttachment(); if ($this->isAttachmentValidPicture($attachment)) { return $attachment; } } //If nothing is available return null return null; } /** * Checks if a attachment is exising and a valid picture. * @param Attachment|null $attachment The attachment that should be checked. * @return bool True if the attachment is valid. */ protected function isAttachmentValidPicture(?Attachment $attachment) : bool { return $attachment !== null && $attachment->isPicture() && $this->attachmentHelper->isFileExisting($attachment); } }