Skip to content

Commit

Permalink
refactor like it'ts the last day on earth
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Apr 22, 2015
1 parent 69ccf22 commit 5949141
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 130 deletions.
18 changes: 11 additions & 7 deletions src/ImageManipulators/GlideImageManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,27 @@ private function createProfileImages(Media $media, $originalFile, $originalPath)
foreach ($imageProfiles as $profileName => $conversionParameters) {
$shouldBeQueued = $this->determineShouldBeQueued($conversionParameters);

$conversionParameters = $this->unsetQueueKey($conversionParameters);
$imageConversionParameters = $this->unsetQueueKey($conversionParameters);

if (! $shouldBeQueued) {
$outputFile = $this->determineOutputFileName($media, $originalPath, $profileName, $conversionParameters);
$outputFile = $this->determineOutputFileName($media, $originalPath, $profileName, $imageConversionParameters);

if ($shouldBeQueued) {

$this->convertImage($originalFile, $conversionParameters, $outputFile);
} else {
Queue::push(
GlideQueueHandler::class,
[
'sourceFile' => $originalFile,
'conversionParameters' => $conversionParameters,
'outputFile' => $this->determineOutputFileName($media, $originalPath, $profileName, $conversionParameters),
'conversionParameters' => $imageConversionParameters,
'outputFile' => $outputFile,
],
'media_queue'
);

return;
}

$this->convertImage($originalFile, $imageConversionParameters, $outputFile);

}
}

Expand Down
57 changes: 45 additions & 12 deletions src/MediaLibraryModel/MediaLibraryModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static function boot()
});
}

/**
* Set the polymorphic relation.
*
* @return mixed
*/
public function media()
{
return $this->morphMany('Spatie\MediaLibrary\Models\Media', 'content');
}


/**
* Get media collection by its collectionName.
*
Expand Down Expand Up @@ -47,6 +58,26 @@ public function getFirstMedia($collectionName, $filters = [])
return (count($media) ? $media[0] : false);
}

/**
* Get the url of the image for the given profileName
* for first media for the given collectionName
*
* @param $collectionName
* @param $profileName
* @return bool
*/
public function getFirstMediaURL($collectionName, $profileName)
{
$media = $this->getFirstMedia($collectionName);

if ($media)
{
return $media->getURL($profileName);
}

return false;
}

/**
* Add media to media collection from a given file.
*
Expand Down Expand Up @@ -82,19 +113,13 @@ public function removeMedia($id)
* Update a media collection by deleting and inserting again with new values.
*
* @param array $newMediaArray
* @param $collectionName
* @param string $collectionName
*
* @throws Exception
*/
public function updateMedia(array $newMediaArray, $collectionName)
{
$mediaItems = new Collection($newMediaArray);

foreach ($this->getMedia($collectionName, []) as $currentMedia) {
if (! in_array($currentMedia->id, $mediaItems->lists('id'))) {
$this->removeMedia($currentMedia->id);
}
}
$this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);

$orderCounter = 0;

Expand All @@ -117,13 +142,21 @@ public function updateMedia(array $newMediaArray, $collectionName)
}
}


/**
* Set the polymorphic relation.
*
* @param array $newMediaArray
* @param $collectionName
* @return mixed
*/
public function media()
private function removeMediaItemsNotPresentInArray(array $newMediaArray, $collectionName)
{
return $this->morphMany('Spatie\MediaLibrary\Models\Media', 'content');
$newMediaItems = new Collection($newMediaArray);

foreach ($this->getMedia($collectionName, []) as $currentMedia) {
if (!in_array($currentMedia->id, $newMediaItems->lists('id'))) {
$this->removeMedia($currentMedia->id);
}
}
return $currentMedia;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
<?php namespace Spatie\MediaLibrary\Repositories;
<?php namespace Spatie\MediaLibrary;

use Spatie\MediaLibrary\FileSystems\FileSystemInterface;
use Spatie\MediaLibrary\MediaLibraryModel\MediaLibraryModelInterface;
use Spatie\MediaLibrary\Models\Media;
use Carbon\Carbon;

class MediaLibraryRepository implements MediaLibraryRepositoryInterface
class MediaLibraryRepository
{
protected $fileSystem;

/**
* @param FileSystemInterface $fileSystem
*/
public function __construct(FileSystemInterface $fileSystem)
{
$this->fileSystem = $fileSystem;
}

/**
* Add a new media to a Models mediaCollection.
* Add a file to the media collection of the given model
*
* @param $file
* @param MediaLibraryModelInterface $model
* @param $collectionName
* @param bool $preserveOriginal
* @param bool $addAsTemporary
* @param bool $preserveOriginal if this is set to true the file will be removed from it's original location
* @param bool $addAsTemporary
*
* @return Media
*/
public function add($file, MediaLibraryModelInterface $model, $collectionName, $preserveOriginal = false, $addAsTemporary = false)
{
$media = $this->createMediaFromFile($file, $collectionName, $addAsTemporary);
$media = $this->createMediaForFile($file, $collectionName, $addAsTemporary);

$model->media()->save($media);

Expand Down Expand Up @@ -88,9 +91,9 @@ public function order($orderArray, MediaLibraryModelInterface $model)
*/
public function getCollection(MediaLibraryModelInterface $model, $collectionName, $filters)
{
$media = $this->loadMedia($model, $collectionName);
$mediaItems = $this->loadMedia($model, $collectionName);

$media = $this->addURLToMediaProfile($media);
$media = $this->addURLsToMediaProfile($mediaItems);

$media = $this->applyFiltersToMedia($media, $filters);

Expand Down Expand Up @@ -131,15 +134,15 @@ public function regenerateDerivedFiles($media)
}

/**
* Create a new media-records from a filepath and collectionName.
* Create a new media-record for a file.
*
* @param $file
* @param $collectionName
* @param $addAsTemporary
*
* @return Media
*/
private function createMediaFromFile($file, $collectionName, $addAsTemporary)
private function createMediaForFile($file, $collectionName, $addAsTemporary)
{
$pathParts = pathinfo($file);

Expand All @@ -158,56 +161,32 @@ private function createMediaFromFile($file, $collectionName, $addAsTemporary)
}

/**
* Load media by collectionName.
* Apply given filters on media.
*
* @param MediaLibraryModelInterface $model
* @param $collectionName
* @param $media
* @param $filters
*
* @return mixed
*/
private function loadMedia(MediaLibraryModelInterface $model, $collectionName)
private function applyFiltersToMedia($media, $filters)
{
if ($this->mediaIsPreloaded($model)) {
$media = $model->media->filter(function ($mediaItem) use ($collectionName) {
return $mediaItem->collection_name == $collectionName;
})->sortBy(function ($media) {
return $media->order_column;
})->values();

return $media;
foreach ($filters as $filterProperty => $filterValue) {
$media = $media->filter(function ($media) use ($filterProperty, $filterValue) {
return $media->$filterProperty == $filterValue;
});
}

$media = $model->media()
->where('collection_name', $collectionName)
->orderBy('order_column')->get();

return $media;
}

/**
* Check if the media is preloaded.
*
* @param MediaLibraryModelInterface $model
*
* @return bool
*/
private function mediaIsPreloaded(MediaLibraryModelInterface $model)
{
if (isset($model->media)) {
return true;
}

return false;
}

/**
* Add URL to profile-image media.
*
* @param $media
*
* @return mixed
*/
private function addURLToMediaProfile($media)
private function addURLsToMediaProfile($media)
{
foreach ($media as $mediaKey => $mediaItem) {
$media[$mediaKey] = $this->addURLsToMediaItem($mediaItem);
Expand All @@ -233,21 +212,49 @@ private function addURLsToMediaItem($mediaItem)
}

/**
* Apply given filters on media.
* Load media by collectionName.
*
* @param $media
* @param $filters
* @param MediaLibraryModelInterface $model
* @param $collectionName
*
* @return mixed
*/
private function applyFiltersToMedia($media, $filters)
private function loadMedia(MediaLibraryModelInterface $model, $collectionName)
{
foreach ($filters as $filterProperty => $filterValue) {
$media = $media->filter(function ($media) use ($filterProperty, $filterValue) {
return $media->$filterProperty == $filterValue;
});
if ($this->mediaIsPreloaded($model)) {
$media = $model->media->filter(function ($mediaItem) use ($collectionName) {
return $mediaItem->collection_name == $collectionName;
})->sortBy(function ($media) {
return $media->order_column;
})->values();

return $media;
}

$media = $model->media()
->where('collection_name', $collectionName)
->orderBy('order_column')->get();

return $media;
}

/**
* Check if the media is preloaded.
*
* @param MediaLibraryModelInterface $model
*
* @return bool
*/
private function mediaIsPreloaded(MediaLibraryModelInterface $model)
{
if (isset($model->media)) {
return true;
}

return false;
}




}
2 changes: 1 addition & 1 deletion src/MediaLibraryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Spatie\MediaLibrary\FileSystems\LocalFileSystem;
use Spatie\MediaLibrary\FileSystems\FileSystemInterface;
use Spatie\MediaLibrary\ImageManipulators\ImageManipulatorInterface;
use Spatie\MediaLibrary\Repositories\MediaLibraryRepository;
use Spatie\MediaLibrary\MediaLibraryRepository;

class MediaLibraryServiceProvider extends ServiceProvider
{
Expand Down
59 changes: 0 additions & 59 deletions src/Repositories/MediaLibraryRepositoryInterface.php

This file was deleted.

0 comments on commit 5949141

Please sign in to comment.