Skip to content

Commit

Permalink
Refactored and removed useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jul 30, 2024
1 parent f6921f1 commit f75b14b
Showing 1 changed file with 21 additions and 55 deletions.
76 changes: 21 additions & 55 deletions app/Services/Campaign/Import/Mappers/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected function entity(): void


$this
->image()
->header()
->images()
->gallery();
$this->entity->save();

Expand Down Expand Up @@ -135,80 +134,49 @@ protected function entityThird(): void
;
}

protected function image(): self
/**
* Migrate old entity image system to the gallery.
*/
protected function images(): self
{
$img = Arr::get($this->data, 'entity.image_path');
$imageExt = Str::after($img, '.');

if (empty($img)) {
return $this;
}

if (!Storage::disk('local')->exists($this->path . $img)) {
//dd('image ' . $this->path . $img . ' doesnt exist');
return $this;
$oldImages = ['image_path', 'header_image'];
foreach ($oldImages as $old) {
$this->migrateToGallery($old);
}

//We need to create a new Image to migrate to the new system.
$image = new Image();
$image->campaign_id = $this->campaign->id;
$image->id = Str::uuid();
$image->ext = $imageExt;
$image->name = $this->entity->name;
$image->visibility_id = 1;
$size = Storage::disk('local')->size($this->path . $img);
$image->size = (int) ceil($size / 1024); // kb
$image->save();

// An image needs the image saved locally
$destination = 'campaigns/' . $this->campaign->id . '/' . $image->id . '.' . $imageExt;

// Upload the file to s3 using streams
Storage::writeStream($destination, Storage::disk('local')->readStream($this->path . $img));

ImportIdMapper::putGallery($image->id, $image->id);

$this->entity->image_uuid = ImportIdMapper::getGallery($image->id);
;

return $this;
}

protected function header(): self
{
$img = Arr::get($this->data, 'entity.header_image');
$imageExt = Str::after($img, '.');
protected function migrateToGallery(string $old): self
{
$img = Arr::get($this->data, 'entity.' . $old);

if (empty($img)) {
if (empty($img) || !Storage::disk('local')->exists($this->path . $img)) {
return $this;
}

if (!Storage::disk('local')->exists($this->path . $img)) {
//dd('header image ' . $this->path . $img . ' doesnt exist');
return $this;
}
$imageExt = Str::after($img, '.');

//We need to create a new Image to migrate to the new system.
$image = new Image();
$image->campaign_id = $this->campaign->id;
$image->id = Str::uuid();
$image->ext = $imageExt;
$image->name = $this->entity->name . ' header';
$image->name = $this->entity->name;
$image->visibility_id = 1;
$size = Storage::disk('local')->size($this->path . $img);
$image->size = (int) ceil($size / 1024); // kb
$image->save();

// An image needs the image saved locally
$destination = 'campaigns/' . $this->campaign->id . '/' . $image->id . '.' . $imageExt;

// Upload the file to s3 using streams
Storage::writeStream($destination, Storage::disk('local')->readStream($this->path . $img));

Storage::writeStream($image->path, Storage::disk('local')->readStream($this->path . $img));
ImportIdMapper::putGallery($image->id, $image->id);

$this->entity->header_uuid = ImportIdMapper::getGallery($image->id);
;
if ($old == 'image_path') {
$this->entity->image_uuid = ImportIdMapper::getGallery($image->id);
} else {
$this->entity->header_uuid = ImportIdMapper::getGallery($image->id);
}

return $this;
}

Expand All @@ -217,12 +185,10 @@ protected function gallery(): self
$image = Arr::get($this->data, 'entity.image_uuid');
if (!empty($image)) {
$this->entity->image_uuid = ImportIdMapper::getGallery($image);
;
}
$image = Arr::get($this->data, 'entity.header_uuid');
if (!empty($image)) {
$this->entity->header_uuid = ImportIdMapper::getGallery($image);
;
}
return $this;
}
Expand Down

0 comments on commit f75b14b

Please sign in to comment.