Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
storeFile example
if
(
$originalFileName
=== null
)
{
$originalFileName
=
$this
->fileService->
generateFilename
(
$profileEntity
)
;
}
if
(
$profileEntity
->
getMapping
(
)
!== null
)
{
$mappings
= MappingCollection::
fromIterable
(
$profileEntity
->
getMapping
(
)
)
;
$profileEntity
->
setMapping
(
$mappings
->
sortByPosition
(
)
)
;
}
$fileEntity
=
$this
->fileService->
storeFile
(
$context
,
$expireDate
, null,
$originalFileName
,
$activity
,
$destinationPath
)
;
return
$this
->
createLog
(
$context
,
$activity
,
$fileEntity
,
$profileEntity
,
$config
)
;
}
/** * @param Config $config */
public
function
prepareImport
(
Context
$context
,
string
$profileId
,
\DateTimeInterface
$expireDate
,
public
function
testStoreFile
(
)
: void
{
/** @var EntityRepository $fileRepository */
$fileRepository
=
$this
->
getContainer
(
)
->
get
(
'import_export_file.repository'
)
;
$fileService
=
new
FileService
(
$this
->
getContainer
(
)
->
get
(
'shopware.filesystem.private'
)
,
$fileRepository
)
;
$storedFile
=
$fileService
->
storeFile
(
Context::
createDefaultContext
(
)
,
new
\
DateTimeImmutable
(
)
,
null,
'testfile.csv',
ImportExportLogEntity::ACTIVITY_IMPORT
)
;
static
::
assertSame
(
'testfile.csv',
$storedFile
->
getOriginalName
(
)
)
;
$dbFile
=
$fileRepository
->
search
(
new
Criteria
(
[
$storedFile
->
getId
(
)
]
)
, Context::
createDefaultContext
(
)
)
->
first
(
)
;
static
::
assertNotNull
(
$dbFile
)
;
$headers
=
[
]
;
$mappings
= MappingCollection::
fromIterable
(
$mappings
)
->
sortByPosition
(
)
;
/** @var Mapping $mapping */
foreach
(
$mappings
as
$mapping
)
{
$headers
[
$mapping
->
getMappedKey
(
)
]
= '';
}
// create the file
$expireDate
=
new
\
DateTimeImmutable
(
)
;
$expireDate
=
$expireDate
->
modify
(
'+1 hour'
)
;
$fileEntity
=
$this
->fileService->
storeFile
(
$context
,
$expireDate
,
null,
$profile
->
getSourceEntity
(
)
. ':' .
$profile
->
getName
(
)
. '.csv',
ImportExportLogEntity::ACTIVITY_TEMPLATE
)
;
// write to the file
$targetFile
=
$fileEntity
->
getPath
(
)
;
$writer
=
$this
->fileService->
getWriter
(
)
;
$writer
->
append
(
$config
,
$headers
, 0
)
;