Skip to content

Commit

Permalink
Merge pull request #35 from FarhanShares/count-of-attach-detach
Browse files Browse the repository at this point in the history
Attachment & detachment count or null
  • Loading branch information
FarhanShares authored Mar 27, 2021
2 parents 3953d67 + 1a4c549 commit 8fac0f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FarhanShares\MediaMan\Traits;

use Throwable;
use FarhanShares\MediaMan\MediaChannel;
use FarhanShares\MediaMan\Models\Media;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function getFirstMediaUrl(?string $channel = 'default', string $conversio
* @param mixed $media
* @param string $channel
* @param array $conversions
* @return void
* @return int|null
*/
public function attachMedia($media, string $channel = 'default', array $conversions = [])
{
Expand Down Expand Up @@ -114,10 +115,19 @@ public function attachMedia($media, string $channel = 'default', array $conversi
});
}

// todo: use sync($ids, false)
$this->media()->attach($ids, [
'channel' => $channel,
]);

$mappedIds = [];
foreach ($ids as $id) {
$mappedIds[$id] = ['channel' => $channel];
}

try {
$res = $this->media()->sync($mappedIds, false);
$attached = count($res['attached']);
return $attached > 0 ? $attached : null;
} catch (Throwable $th) {
return null;
}
}

/**
Expand Down Expand Up @@ -179,11 +189,13 @@ public function getMediaChannel(string $name)
* Detach the specified media.
*
* @param mixed $media
* @return void
* @return int|null
*/
public function detachMedia($media = null)
{
$this->media()->detach($media);
$count = $this->media()->detach($media);

return $count > 0 ? $count : null;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/HasMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ function ($media) {
);
}

/** @test */
public function it_returns_number_of_attached_media_or_null_while_associating()
{
$media = factory(Media::class)->create();

$attached = $this->subject->attachMedia($media, 'custom');

$this->assertEquals(1, $attached);

// todo: couldn't test null return type with sqlite test environment
// todo: as sqlite doesn't have relationship, it won't fail, but it works on relational db
// try attaching a non-existing media record
// $attached = $this->subject->attachMedia(5, 'custom');
// $this->assertEquals(null, $attached);
}

/** @test */
public function it_returns_number_of_detached_media_or_null_while_disassociating()
{
$media = factory(Media::class)->create();
$this->subject->attachMedia($media, 'custom');

$detached = $this->subject->detachMedia($media);

$this->assertEquals(1, $detached);

// try detaching a non-existing media record
$detached = $this->subject->detachMedia(100);
$this->assertEquals(null, $detached);
}

/** @test */
public function it_will_perform_the_given_conversions_when_media_is_attached()
{
Expand Down

0 comments on commit 8fac0f4

Please sign in to comment.