Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getFileType example
$expireDateString
=
$input
->
getArgument
(
'expireDate'
)
;
try
{
$expireDate
=
new
\
DateTimeImmutable
(
$expireDateString
)
;
}
catch
(
\Exception
)
{
throw
new
\
InvalidArgumentException
(
sprintf
(
'"%s" is not a valid date. Please use format Y-m-d',
$expireDateString
)
)
;
}
$file
=
new
UploadedFile
(
$filePath
,
basename
(
(string)
$filePath
)
,
$profile
->
getFileType
(
)
)
;
$doRollback
=
$rollbackOnError
&& !
$dryRun
;
if
(
$doRollback
)
{
$this
->connection->
setNestTransactionsWithSavepoints
(
true
)
;
$this
->connection->
beginTransaction
(
)
;
}
$log
=
$this
->initiationService->
prepareImport
(
$context
,
$profile
->
getId
(
)
,
$expireDate
,
'documentDate' =>
$operation
->
getConfig
(
)
[
'documentDate'
]
??
$now
,
'documentNumber' =>
$number
,
'custom' =>
[
'invoiceNumber' =>
$number
,
]
,
]
)
;
// create version of order to ensure the document stays the same even if the order changes
$operation
->
setOrderVersionId
(
$this
->orderRepository->
createVersion
(
$orderId
,
$context
, 'document'
)
)
;
if
(
$operation
->
isStatic
(
)
)
{
$doc
=
new
RenderedDocument
(
'',
$number
,
$config
->
buildName
(
)
,
$operation
->
getFileType
(
)
,
$config
->
jsonSerialize
(
)
)
;
$result
->
addSuccess
(
$orderId
,
$doc
)
;
continue
;
}
/** @var LocaleEntity $locale */
$locale
=
$order
->
getLanguage
(
)
->
getLocale
(
)
;
$html
=
$this
->documentTemplateRenderer->
render
(
$template
,
[
$config
->
merge
(
[
'documentDate' =>
$operation
->
getConfig
(
)
[
'documentDate'
]
??
(
new
\
DateTime
(
)
)
->
format
(
Defaults::STORAGE_DATE_TIME_FORMAT
)
,
'documentNumber' =>
$number
,
'custom' =>
[
'creditNoteNumber' =>
$number
,
'invoiceNumber' =>
$referenceDocumentNumber
,
]
,
]
)
;
if
(
$operation
->
isStatic
(
)
)
{
$doc
=
new
RenderedDocument
(
'',
$number
,
$config
->
buildName
(
)
,
$operation
->
getFileType
(
)
,
$config
->
jsonSerialize
(
)
)
;
$result
->
addSuccess
(
$orderId
,
$doc
)
;
continue
;
}
$price
=
$this
->
calculatePrice
(
$creditItems
,
$order
)
;
/** @var LocaleEntity $locale */
$locale
=
$order
->
getLanguage
(
)
->
getLocale
(
)
;
$html
=
$this
->documentTemplateRenderer->
render
(
}
public
function
create
(
ImportExportLogEntity
$logEntity
)
: AbstractWriter
{
return
new
XmlFileWriter
(
$this
->filesystem
)
;
}
public
function
supports
(
ImportExportLogEntity
$logEntity
)
: bool
{
return
$logEntity
->
getActivity
(
)
=== ImportExportLogEntity::ACTIVITY_EXPORT
&&
$logEntity
->
getProfile
(
)
->
getFileType
(
)
=== 'text/xml';
}
}
UploadedFile
$file
,
array
$config
=
[
]
,
bool
$dryRun
= false
)
: ImportExportLogEntity
{
$profileEntity
=
$this
->
findProfile
(
$context
,
$profileId
)
;
if
(
!\
in_array
(
$profileEntity
->
getType
(
)
,
[
ImportExportProfileEntity::TYPE_IMPORT, ImportExportProfileEntity::TYPE_IMPORT_EXPORT
]
, true
)
)
{
throw
new
ProfileWrongTypeException
(
$profileEntity
->
getId
(
)
,
$profileEntity
->
getType
(
)
)
;
}
$type
=
$this
->fileService->
detectType
(
$file
)
;
if
(
$type
!==
$profileEntity
->
getFileType
(
)
)
{
throw
new
UnexpectedFileTypeException
(
$file
->
getClientMimeType
(
)
,
$profileEntity
->
getFileType
(
)
)
;
}
$fileEntity
=
$this
->fileService->
storeFile
(
$context
,
$expireDate
,
$file
->
getPathname
(
)
,
$file
->
getClientOriginalName
(
)
, ImportExportLogEntity::ACTIVITY_IMPORT
)
;
$activity
=
$dryRun
? ImportExportLogEntity::ACTIVITY_DRYRUN : ImportExportLogEntity::ACTIVITY_IMPORT;
return
$this
->
createLog
(
$context
,
$activity
,
$fileEntity
,
$profileEntity
,
$config
)
;
}
public
function
cancel
(
Context
$context
, string
$logId
)
: void
{
$this
->repository->
create
(
array_values
(
$data
)
,
$this
->context
)
;
foreach
(
$data
as
$expect
)
{
$id
=
$expect
[
'id'
]
;
/** @var ImportExportProfileEntity $importExportProfile */
$importExportProfile
=
$this
->repository->
search
(
new
Criteria
(
[
$id
]
)
,
$this
->context
)
->
get
(
$id
)
;
static
::
assertEquals
(
$expect
[
'name'
]
,
$importExportProfile
->
getName
(
)
)
;
static
::
assertEquals
(
$expect
[
'label'
]
,
$importExportProfile
->
getLabel
(
)
)
;
static
::
assertEquals
(
$expect
[
'systemDefault'
]
,
$importExportProfile
->
getSystemDefault
(
)
)
;
static
::
assertEquals
(
$expect
[
'sourceEntity'
]
,
$importExportProfile
->
getSourceEntity
(
)
)
;
static
::
assertEquals
(
$expect
[
'fileType'
]
,
$importExportProfile
->
getFileType
(
)
)
;
static
::
assertEquals
(
$expect
[
'delimiter'
]
,
$importExportProfile
->
getDelimiter
(
)
)
;
static
::
assertEquals
(
$expect
[
'enclosure'
]
,
$importExportProfile
->
getEnclosure
(
)
)
;
static
::
assertEquals
(
$expect
[
'mapping'
]
,
$importExportProfile
->
getMapping
(
)
)
;
}
}
public
function
testImportExportProfileReadNoResult
(
)
: void
{
$num
= 3;
$data
=
$this
->
prepareImportExportProfileTestData
(
$num
)
;
$config
->
merge
(
[
'documentNumber' =>
$number
,
'documentDate' =>
$operation
->
getConfig
(
)
[
'documentDate'
]
??
$now
,
'custom' =>
[
'deliveryNoteNumber' =>
$number
,
'deliveryDate' =>
$customConfig
[
'deliveryDate'
]
??
$now
,
'deliveryNoteDate' =>
$customConfig
[
'deliveryNoteDate'
]
??
$now
,
]
,
]
)
;
if
(
$operation
->
isStatic
(
)
)
{
$doc
=
new
RenderedDocument
(
'',
$number
,
$config
->
buildName
(
)
,
$operation
->
getFileType
(
)
,
$config
->
jsonSerialize
(
)
)
;
$result
->
addSuccess
(
$orderId
,
$doc
)
;
continue
;
}
$deliveries
= null;
if
(
$order
->
getDeliveries
(
)
)
{
$deliveries
=
$order
->
getDeliveries
(
)
->
first
(
)
;
}
/** @var LocaleEntity $locale */
public
function
__construct
(
private
readonly FilesystemOperator
$filesystem
)
{
}
public
function
create
(
ImportExportLogEntity
$logEntity
)
: AbstractWriter
{
return
new
CsvFileWriter
(
$this
->filesystem
)
;
}
public
function
supports
(
ImportExportLogEntity
$logEntity
)
: bool
{
return
$logEntity
->
getProfile
(
)
->
getFileType
(
)
=== 'text/csv';
}
}
#[Package('system-settings')]
class
CsvReaderFactory
extends
AbstractReaderFactory
{
public
function
create
(
ImportExportLogEntity
$logEntity
)
: AbstractReader
{
return
new
CsvReader
(
)
;
}
public
function
supports
(
ImportExportLogEntity
$logEntity
)
: bool
{
return
$logEntity
->
getProfile
(
)
->
getFileType
(
)
=== 'text/csv';
}
}
return
$file
->
getClientMimeType
(
)
;
}
public
function
getWriter
(
)
: AbstractWriter
{
return
$this
->writer;
}
public
function
generateFilename
(
ImportExportProfileEntity
$profile
)
: string
{
$extension
=
$profile
->
getFileType
(
)
=== 'text/xml' ? 'xml' : 'csv';
$timestamp
=
date
(
'Ymd-His'
)
;
$label
=
$profile
->
getTranslation
(
'label'
)
;
\
assert
(
\
is_string
(
$label
)
)
;
return
sprintf
(
'%s_%s.%s',
$label
,
$timestamp
,
$extension
)
;
}
public
function
updateFile
(
Context
$context
, string
$fileId
, array
$data
)
: void
{
$data
[
'id'
]
=
$fileId
;
$config
->
merge
(
[
'documentDate' =>
$operation
->
getConfig
(
)
[
'documentDate'
]
??
$now
,
'documentNumber' =>
$number
,
'custom' =>
[
'stornoNumber' =>
$number
,
'invoiceNumber' =>
$referenceDocumentNumber
,
]
,
]
)
;
if
(
$operation
->
isStatic
(
)
)
{
$doc
=
new
RenderedDocument
(
'',
$number
,
$config
->
buildName
(
)
,
$operation
->
getFileType
(
)
,
$config
->
jsonSerialize
(
)
)
;
$result
->
addSuccess
(
$orderId
,
$doc
)
;
continue
;
}
/** @var LocaleEntity $locale */
$locale
=
$order
->
getLanguage
(
)
->
getLocale
(
)
;
$html
=
$this
->documentTemplateRenderer->
render
(
$template
,
[
'order' =>
$order
,
$this
->
checkDocumentNumberAlreadyExits
(
$documentType
,
$document
->
getNumber
(
)
,
$context
,
$operation
->
getDocumentId
(
)
)
;
$deepLinkCode
= Random::
getAlphanumericString
(
32
)
;
$id
=
$operation
->
getDocumentId
(
)
?? Uuid::
randomHex
(
)
;
$mediaId
=
$this
->
resolveMediaId
(
$operation
,
$context
,
$document
)
;
$records
[
]
=
[
'id' =>
$id
,
'documentTypeId' =>
$documentTypeId
,
'fileType' =>
$operation
->
getFileType
(
)
,
'orderId' =>
$orderId
,
'orderVersionId' =>
$operation
->
getOrderVersionId
(
)
,
'static' =>
$operation
->
isStatic
(
)
,
'documentMediaFileId' =>
$mediaId
,
'config' =>
$document
->
getConfig
(
)
,
'deepLinkCode' =>
$deepLinkCode
,
'referencedDocumentId' =>
$operation
->
getReferencedDocumentId
(
)
,
]
;
$result
->
addSuccess
(
new
DocumentIdStruct
(
$id
,
$deepLinkCode
,
$mediaId
)
)
;
}
catch
(
\Throwable
$exception
)
{
/** @var DocumentEntity $document */
$document
=
$this
->documentRepository
->
search
(
$criteria
,
$this
->context
)
->
get
(
$documentStruct
->
getId
(
)
)
;
static
::
assertNotNull
(
$document
)
;
static
::
assertSame
(
$this
->orderId,
$document
->
getOrderId
(
)
)
;
static
::
assertNotNull
(
$document
->
getDocumentType
(
)
)
;
static
::
assertSame
(
Defaults::LIVE_VERSION,
$document
->
getOrderVersionId
(
)
)
;
static
::
assertSame
(
DeliveryNoteRenderer::TYPE,
$document
->
getDocumentType
(
)
->
getTechnicalName
(
)
)
;
static
::
assertSame
(
FileTypes::PDF,
$document
->
getFileType
(
)
)
;
}
public
function
testGenerateEmpty
(
)
: void
{
$documentStruct
=
$this
->documentGenerator->
generate
(
DeliveryNoteRenderer::TYPE,
[
]
,
$this
->context
)
->
getSuccess
(
)
;
static
::
assertCount
(
0,
$documentStruct
)
;
$invalidOrderId
= Uuid::
randomHex
(
)
;
$documentStruct
=
$this
->documentGenerator->
generate
(
DeliveryNoteRenderer::TYPE,
[