Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
provide example
$fileExtension
=
$request
->query->
getString
(
'extension'
)
;
$mediaId
=
$request
->query->
has
(
'mediaId'
)
?
$request
->query->
getString
(
'mediaId'
)
: null;
if
(
$preferredFileName
=== ''
)
{
throw
MediaException::
emptyMediaFilename
(
)
;
}
if
(
$fileExtension
=== ''
)
{
throw
MediaException::
missingFileExtension
(
)
;
}
$name
=
$this
->fileNameProvider->
provide
(
$preferredFileName
,
$fileExtension
,
$mediaId
,
$context
)
;
return
new
JsonResponse
(
[
'fileName' =>
$name
]
)
;
}
}
$this
->context = Context::
createDefaultContext
(
)
;
$this
->
setFixtureContext
(
$this
->context
)
;
}
public
function
testItReturnsOriginalName
(
)
: void
{
$media
=
$this
->
getEmptyMedia
(
)
;
$original
= 'test';
$new
=
$this
->nameProvider->
provide
(
$original
, 'png',
$media
->
getId
(
)
,
$this
->context
)
;
static
::
assertEquals
(
$original
,
$new
)
;
}
public
function
testItGeneratesNewName
(
)
: void
{
$existing
=
$this
->
getJpg
(
)
;
$media
=
$this
->
getEmptyMedia
(
)
;
$new
=
$this
->nameProvider->
provide
(
if
(
!
$provider
)
{
$exceptions
->
add
(
$providerEntity
->
getIdentifier
(
)
,
new
NotFoundHttpException
(
\
sprintf
(
'No tax provider found for identifier %s',
$providerEntity
->
getIdentifier
(
)
)
)
)
;
continue
;
}
try
{
$taxProviderStruct
=
$provider
->
provide
(
$cart
,
$context
)
;
}
catch
(
\Throwable
$e
)
{
$exceptions
->
add
(
$providerEntity
->
getIdentifier
(
)
,
$e
)
;
continue
;
}
// taxes given - no need to continue
if
(
$taxProviderStruct
->
declaresTaxes
(
)
)
{
return
$taxProviderStruct
;
}
}
if
(
!
$item
[
'mediaFile'
]
instanceof MediaFile
)
{
throw
MediaException::
missingFile
(
$item
[
'media'
]
[
'id'
]
)
;
}
try
{
$this
->fileSaver->
persistFileToMedia
(
$item
[
'mediaFile'
]
,
$item
[
'basename'
]
,
$item
[
'media'
]
[
'id'
]
,
$context
)
;
}
catch
(
MediaException
$e
)
{
if
(
$e
->
getErrorCode
(
)
!== MediaException::MEDIA_DUPLICATED_FILE_NAME
)
{
throw
$e
;
}
$newFileName
=
$this
->fileNameProvider->
provide
(
$item
[
'basename'
]
,
$item
[
'mediaFile'
]
->
getFileExtension
(
)
,
null,
$context
)
;
$this
->fileSaver->
persistFileToMedia
(
$item
[
'mediaFile'
]
,
$newFileName
,
$item
[
'media'
]
[
'id'
]
,
$context
)
;
}
}
}
$themeData
[
'media'
]
=
$mediaIds
;
$writeContext
)
;
$this
->mediaUpdater->
persistFileToMedia
(
new
MediaFile
(
$file
,
(string)
mime_content_type
(
$file
)
,
pathinfo
(
$file
, \PATHINFO_EXTENSION
)
,
(int)
filesize
(
$file
)
)
,
$this
->fileNameProvider->
provide
(
pathinfo
(
$file
, \PATHINFO_FILENAME
)
,
pathinfo
(
$file
, \PATHINFO_EXTENSION
)
,
$mediaId
,
$context
->
getContext
(
)
)
,
$mediaId
,
$context
->
getContext
(
)
)
;
$context
->
getConsole
(
)
->
progressAdvance
(
1
)
;
}