Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
rratsun committed Jul 10, 2023
1 parent c684178 commit 9ed1cd8
Showing 1 changed file with 31 additions and 43 deletions.
74 changes: 31 additions & 43 deletions app/Services/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,41 @@ public function createEntity($attachment)
{
$entity = parent::createEntity($attachment);

// validate
$this->validateAttachment($entity, $attachment);

// create asset
$this->createAsset($entity, $attachment);
if ($this->attachmentHasAsset($attachment)) {
$this->validateAttachment($entity, $attachment);
$this->createAsset($entity, $attachment);
}

return $entity;
}

/**
* @param Entity $entity
* @param \stdClass $data
*
* @throws BadRequest
*/
protected function validateAttachment(Entity $entity, \stdClass $data): void
public function attachmentHasAsset(\stdClass $input = null): bool
{
if (!empty($input) && property_exists($input, 'relatedType') && $input->relatedType === 'Note') {
return false;
}

if (property_exists($input, 'relatedType') && property_exists($input, 'field')) {
if ($this->getMetadata()->get(['entityDefs', $input->relatedType, 'fields', $input->field, 'noAsset'])) {
return false;
}
}

return true;
}

public function createAsset(Entity $entity, ?\stdClass $attachment = null): void
{
if (empty($entity->getAsset())) {
$type = null;
if (!empty($attachment) && !empty($attachment->modelAttributes->attributeAssetType)) {
$type = $attachment->modelAttributes->attributeAssetType;
}
$this->getRepository()->createAsset($entity, false, $type);
}
}

public function validateAttachment(Entity $entity, \stdClass $data): void
{
$parentType = property_exists($data, 'parentType') ? $data->parentType : '';
$relatedType = property_exists($data, 'relatedType') ? $data->relatedType : '';
Expand Down Expand Up @@ -281,35 +300,4 @@ private function getPosition($width, $height): string

return $result;
}

/**
* @return Manager
*/
protected function getFileStorageManager(): Manager
{
return $this->getInjection("fileStorageManager");
}

/**
* @param Entity $entity
* @param \stdClass $attachment
*/
protected function createAsset(Entity $entity, \stdClass $attachment): void
{
if (
property_exists($attachment, 'relatedType')
&& property_exists($attachment, 'field')
&& $this->getMetadata()->get(['entityDefs', $attachment->relatedType, 'fields', $attachment->field, 'noAsset'], false)
) {
return;
}

if (empty($entity->getAsset())) {
$type = null;
if (!empty($attachment->modelAttributes->attributeAssetType)) {
$type = $attachment->modelAttributes->attributeAssetType;
}
$this->getRepository()->createAsset($entity, false, $type);
}
}
}

0 comments on commit 9ed1cd8

Please sign in to comment.