Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setUploadedAt example
}
;
}
private
function
createMedia
(
string
$name
)
: MediaEntity
{
$media
=
new
MediaEntity
(
)
;
$media
->
setUniqueIdentifier
(
Uuid::
randomHex
(
)
)
;
$media
->
setFileName
(
$name
)
;
$media
->
setFileExtension
(
'jpg'
)
;
$media
->
setTitle
(
$name
. ' title'
)
;
$media
->
setUploadedAt
(
new
\
DateTime
(
'16-02-2023 10:00'
)
)
;
$media
->
setFileSize
(
1024 * 1024
)
;
return
$media
;
}
}
public
function
testItEncodesTheUrl
(
)
: void
{
$filter
=
new
UrlEncodingTwigFilter
(
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$uploadTime
=
new
\
DateTime
(
)
;
$utc
=
$uploadTime
->
getTimestamp
(
)
;
$media
=
new
MediaEntity
(
)
;
$media
->
setId
(
Uuid::
randomHex
(
)
)
;
$media
->
setMimeType
(
'image/png'
)
;
$media
->
setFileExtension
(
'png'
)
;
$media
->
setUploadedAt
(
$uploadTime
)
;
$media
->
setFileName
(
'(image with spaces and brackets)'
)
;
$media
->
setUrl
(
$urlGenerator
->
getAbsoluteMediaUrl
(
$media
)
)
;
static
::
assertStringEndsWith
(
"{
$utc
}
/%28image%20with%20spaces%20and%20brackets%29.png",
(string)
$filter
->
encodeMediaUrl
(
$media
)
)
;
}
}
$fileExtension
,
$context
)
;
$this
->
doRenameMedia
(
$currentMedia
,
$destination
,
$context
)
;
}
private
function
doRenameMedia
(
MediaEntity
$currentMedia
, string
$destination
, Context
$context
)
: void
{
$updatedMedia
=
clone
$currentMedia
;
$updatedMedia
->
setFileName
(
$destination
)
;
$updatedMedia
->
setUploadedAt
(
new
\
DateTime
(
)
)
;
try
{
$renamedFiles
=
$this
->
renameFile
(
$this
->urlGenerator->
getRelativeMediaUrl
(
$currentMedia
)
,
$this
->urlGenerator->
getRelativeMediaUrl
(
$updatedMedia
)
,
$this
->
getFileSystem
(
$currentMedia
)
)
;
}
catch
(
\Exception
)
{
throw
MediaException::
couldNotRenameFile
(
$currentMedia
->
getId
(
)
,
(string)
$currentMedia
->
getFileName
(
)
)
;
}