bytic /
MediaLibrary
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace ByTIC\MediaLibrary\Media\Traits; |
||||||
| 4 | |||||||
| 5 | use ByTIC\MediaLibrary\Conversions\ConversionCollection; |
||||||
| 6 | |||||||
| 7 | /** |
||||||
| 8 | * Trait HasConversionsTrait |
||||||
| 9 | * @package ByTIC\MediaLibrary\Media\Traits |
||||||
| 10 | */ |
||||||
| 11 | trait HasConversionsTrait |
||||||
| 12 | { |
||||||
| 13 | protected $conversions = null; |
||||||
| 14 | |||||||
| 15 | protected $conversionNames; |
||||||
| 16 | |||||||
| 17 | 5 | public function getConversionNames(): array |
|||||
| 18 | { |
||||||
| 19 | 5 | if ($this->conversionNames == null) { |
|||||
| 20 | 5 | $this->conversionNames = $this->generateConversionNames(); |
|||||
| 21 | } |
||||||
| 22 | 5 | return $this->conversionNames; |
|||||
| 23 | } |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * @return array |
||||||
| 27 | */ |
||||||
| 28 | 5 | protected function generateConversionNames() |
|||||
| 29 | { |
||||||
| 30 | 5 | $conversions = $this->getConversions(); |
|||||
| 31 | |||||||
| 32 | 5 | $return = []; |
|||||
| 33 | 5 | foreach ($conversions as $conversion) { |
|||||
| 34 | 3 | $return[] = $conversion->getName(); |
|||||
| 35 | } |
||||||
| 36 | 5 | return $return; |
|||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | 5 | public function getConversions(): ConversionCollection |
|||||
| 40 | { |
||||||
| 41 | 5 | if ($this->conversions == null) { |
|||||
| 42 | 5 | $this->conversions = $this->generateConversions(); |
|||||
| 43 | } |
||||||
| 44 | 5 | return $this->conversions; |
|||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | /** |
||||||
| 48 | * @param $name |
||||||
| 49 | * @return bool |
||||||
| 50 | */ |
||||||
| 51 | 2 | public function hasConversion($name) |
|||||
| 52 | { |
||||||
| 53 | 2 | $names = is_array($name) ? $name : [$name]; |
|||||
| 54 | 2 | $conversions = $this->getConversionNames(); |
|||||
| 55 | 2 | foreach ($names as $name) { |
|||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 56 | 2 | if (!in_array($name, $conversions)) { |
|||||
| 57 | 2 | return false; |
|||||
| 58 | } |
||||||
| 59 | } |
||||||
| 60 | 1 | return true; |
|||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * @return ConversionCollection |
||||||
| 65 | */ |
||||||
| 66 | 5 | protected function generateConversions() |
|||||
| 67 | { |
||||||
| 68 | 5 | $collection = $this->getCollection(); |
|||||
|
0 ignored issues
–
show
It seems like
getCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 69 | 5 | $model = $this->getModel(); |
|||||
|
0 ignored issues
–
show
It seems like
getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 70 | 5 | if ($collection && $model) { |
|||||
| 71 | 5 | return $model->getMediaConversions()->forCollection($collection->getName()); |
|||||
| 72 | } |
||||||
| 73 | return new ConversionCollection(); |
||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | |||||||
| 77 | 2 | public function removeConversions() |
|||||
| 78 | { |
||||||
| 79 | 2 | $converstions = $this->getConversionNames(); |
|||||
| 80 | 2 | $converstions[] = 'full'; |
|||||
| 81 | 2 | $filesystem = $this->getFile()->getFilesystem(); |
|||||
|
0 ignored issues
–
show
It seems like
getFile() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 82 | 2 | foreach ($converstions as $converstion) { |
|||||
| 83 | 2 | $path = $this->getPath($converstion); |
|||||
|
0 ignored issues
–
show
It seems like
getPath() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 84 | 2 | if ($filesystem->has($path)) { |
|||||
| 85 | $filesystem->delete($path); |
||||||
| 86 | } |
||||||
| 87 | } |
||||||
| 88 | 2 | } |
|||||
| 89 | } |
||||||
| 90 |