Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getRelativeThumbnailUrl example
$this
->pathnameStrategy->
generatePathHash
(
$media
)
,
$this
->pathnameStrategy->
generatePathCacheBuster
(
$media
)
,
$this
->pathnameStrategy->
generatePhysicalFilename
(
$media
,
$thumbnail
)
,
]
)
;
}
/** * @throws MediaException */
public
function
getAbsoluteThumbnailUrl
(
MediaEntity
$media
, MediaThumbnailEntity
$thumbnail
)
: string
{
return
$this
->filesystem->
publicUrl
(
$this
->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
)
;
}
public
function
reset
(
)
: void
{
}
/** * @param array<string|null> $parts */
private
function
toPathString
(
array
$parts
)
: string
{
$mediaThumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$mediaThumbnailEntity
->
assign
(
[
'width' => 100,
'height' => 100,
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'thumbnail/d0/b3/24/file.jpg_100x100',
$urlGenerator
->
getRelativeThumbnailUrl
(
$mediaEntity
,
$mediaThumbnailEntity
)
)
;
}
public
function
testResetUrlGenerator
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$toBeCreatedSizes
=
new
MediaThumbnailSizeCollection
(
$config
->
getMediaThumbnailSizes
(
)
->
getElements
(
)
)
;
$toBeDeletedThumbnails
=
new
MediaThumbnailCollection
(
$media
->
getThumbnails
(
)
->
getElements
(
)
)
;
foreach
(
$toBeCreatedSizes
as
$thumbnailSize
)
{
foreach
(
$toBeDeletedThumbnails
as
$thumbnail
)
{
if
(
!
$this
->
isSameDimension
(
$thumbnail
,
$thumbnailSize
)
)
{
continue
;
}
if
(
$strict
=== true
&& !
$this
->
getFileSystem
(
$media
)
->
fileExists
(
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
)
)
{
continue
;
}
$toBeDeletedThumbnails
->
remove
(
$thumbnail
->
getId
(
)
)
;
$toBeCreatedSizes
->
remove
(
$thumbnailSize
->
getId
(
)
)
;
continue
2;
}
}
$delete
= \
array_values
(
\
array_map
(
static
fn
(
string
$id
)
=>
[
'id' =>
$id
]
,
$toBeDeletedThumbnails
->
getIds
(
)
)
)
;
$this
->mediaRepository->
update
(
[
[
'id' =>
$png
->
getId
(
)
,
'thumbnails' =>
[
[
'width' => 100,
'height' => 100,
'highDpi' => false,
]
,
]
,
]
]
,
$context
)
;
$oldMediaPath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$png
)
;
$oldThumbnailPath
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$png
,
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 100
]
)
)
;
$this
->
getPublicFilesystem
(
)
->
write
(
$oldMediaPath
, 'test file content'
)
;
$this
->
getPublicFilesystem
(
)
->
write
(
$oldThumbnailPath
, 'test file content'
)
;
$this
->fileSaver->
renameMedia
(
$png
->
getId
(
)
, 'new destination',
$context
)
;
$updatedMedia
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$png
->
getId
(
)
]
)
,
$context
)
->
get
(
$png
->
getId
(
)
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$updatedMedia
)
;
static
::
assertFalse
(
$this
->
getPublicFilesystem
(
)
->
has
(
$oldMediaPath
)
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$this
->urlGenerator->
getRelativeMediaUrl
(
$updatedMedia
)
)
)
;
static
::
assertFalse
(
$this
->
getPublicFilesystem
(
)
->
has
(
$oldThumbnailPath
)
)
;
$mediaThumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$mediaThumbnailEntity
->
assign
(
[
'width' => 100,
'height' => 100,
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'thumbnail/d0/b3/24/file.jpg_100x100',
$urlGenerator
->
getRelativeThumbnailUrl
(
$mediaEntity
,
$mediaThumbnailEntity
)
)
;
}
public
function
testResetUrlGenerator
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
]
,
]
,
]
,
$this
->context
)
;
$media
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$mediaId
]
)
,
$this
->context
)
->
get
(
$mediaId
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$media
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$mediaPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$media
)
;
$thumbnailPath
=
$urlGenerator
->
getRelativeThumbnailUrl
(
$media
,
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 200
]
)
)
;
$resource
=
fopen
(
self::FIXTURE_FILE, 'rb'
)
;
static
::
assertNotFalse
(
$resource
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$mediaPath
,
$resource
)
;
$resource
=
fopen
(
self::FIXTURE_FILE, 'rb'
)
;
static
::
assertNotFalse
(
$resource
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$thumbnailPath
,
$resource
)
;
$this
->mediaRepository->
delete
(
[
[
'id' =>
$mediaId
]
]
,
$this
->context
)
;
$media
=
$this
->mediaRepository->
search
(
$criteria
,
$this
->context
)
->
get
(
$media
->
getId
(
)
)
;
$mediaThumbnailCollection
=
$media
->
getThumbnails
(
)
;
static
::
assertNotNull
(
$mediaThumbnailCollection
)
;
static
::
assertEquals
(
2,
$mediaThumbnailCollection
->
count
(
)
)
;
foreach
(
$mediaThumbnailCollection
as
$thumbnail
)
{
static
::
assertTrue
(
(
$thumbnail
->
getWidth
(
)
=== 300 &&
$thumbnail
->
getHeight
(
)
=== 300
)
||
(
$thumbnail
->
getWidth
(
)
=== 150 &&
$thumbnail
->
getHeight
(
)
=== 150
)
)
;
$path
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$path
)
,
'Thumbnail: ' .
$path
. ' does not exist'
)
;
}
}
public
function
testUpdateThumbnails
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$media
=
$this
->
getPngWithFolder
(
)
;
$mediaResult
=
$this
->mediaRepository->
search
(
$searchCriteria
,
$this
->context
)
;
/** @var MediaEntity $updatedMedia */
$updatedMedia
=
$mediaResult
->
getEntities
(
)
->
first
(
)
;
$thumbnails
=
$updatedMedia
->
getThumbnails
(
)
;
static
::
assertInstanceOf
(
MediaThumbnailCollection::
class
,
$thumbnails
)
;
static
::
assertEquals
(
2,
$thumbnails
->
count
(
)
)
;
foreach
(
$thumbnails
as
$thumbnail
)
{
$thumbnailPath
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
$folder
=
$updatedMedia
->
getMediaFolder
(
)
;
static
::
assertInstanceOf
(
MediaFolderEntity::
class
,
$folder
)
;
static
::
assertInstanceOf
(
MediaFolderConfigurationEntity::
class
,
$folder
->
getConfiguration
(
)
)
;
$sizes
=
$folder
->
getConfiguration
(
)
->
getMediaThumbnailSizes
(
)
;
static
::
assertInstanceOf
(
MediaThumbnailSizeCollection::
class
,
$sizes
)
;
/** * @return array<string, string> */
private
function
renameThumbnail
(
MediaThumbnailEntity
$thumbnail
,
MediaEntity
$currentMedia
,
MediaEntity
$updatedMedia
)
: array
{
return
$this
->
renameFile
(
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$currentMedia
,
$thumbnail
)
,
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$updatedMedia
,
$thumbnail
)
,
$this
->
getFileSystem
(
$currentMedia
)
)
;
}
return
$this
->
getContainer
(
)
->
get
(
'media.repository'
)
->
search
(
new
Criteria
(
[
$mediaId
]
)
, Context::
createDefaultContext
(
)
)
->
get
(
$mediaId
)
;
}
private
function
createThumbnailFile
(
MediaEntity
$media
, string
$service
)
: string
{
$generator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$thumbnail
=
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 200
]
)
;
$thumbnailPath
=
$generator
->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
$this
->
getFilesystem
(
$service
)
->
write
(
$thumbnailPath
, 'foo'
)
;
return
$thumbnailPath
;
}
}
$envelopes
=
$messageBusMock
->
getMessages
(
)
;
static
::
assertCount
(
4,
$envelopes
)
;
static
::
assertEquals
(
$expectedMessageStrict
,
$envelopes
[
0
]
->
getMessage
(
)
)
;
static
::
assertEquals
(
$expectedMessageNonStrict
,
$envelopes
[
1
]
->
getMessage
(
)
)
;
static
::
assertEquals
(
$expectedMessageNonStrict
,
$envelopes
[
2
]
->
getMessage
(
)
)
;
static
::
assertEquals
(
$expectedMessageStrict
,
$envelopes
[
3
]
->
getMessage
(
)
)
;
}
protected
function
assertThumbnailExists
(
MediaEntity
$media
, MediaThumbnailEntity
$thumbnail
)
: void
{
$thumbnailPath
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$thumbnailPath
)
)
;
}
protected
function
createValidMediaFiles
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$mediaPng
=
$this
->
getPngWithFolder
(
)
;
$mediaJpg
=
$this
->
getJpgWithFolder
(
)
;
$publicPaths
=
[
]
;
$thumbnails
=
$this
->
getThumbnails
(
$affected
,
$context
)
;
foreach
(
$thumbnails
as
$thumbnail
)
{
$media
=
$thumbnail
->
getMedia
(
)
;
if
(
$media
=== null
)
{
continue
;
}
if
(
$media
->
isPrivate
(
)
)
{
$privatePaths
[
]
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
}
else
{
$publicPaths
[
]
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
}
}
$this
->
performFileDelete
(
$context
,
$privatePaths
, Visibility::
PRIVATE
)
;
$this
->
performFileDelete
(
$context
,
$publicPaths
, Visibility::
PUBLIC
)
;
$event
->
addSuccess
(
function
D
)
use
(
$thumbnails
,
$context
)
: void
{
$this
->dispatcher->
dispatch
(
new
MediaThumbnailDeletedEvent
(
$thumbnails
,
$context
)
, MediaThumbnailDeletedEvent::EVENT_NAME
)
;
}
)
;
}