Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getApp example
class
AppDeactivatedEventTest
extends
TestCase
{
public
function
testGetter
(
)
: void
{
$app
=
new
AppEntity
(
)
;
$context
= Context::
createDefaultContext
(
)
;
$event
=
new
AppDeactivatedEvent
(
$app
,
$context
)
;
static
::
assertEquals
(
$app
,
$event
->
getApp
(
)
)
;
static
::
assertEquals
(
$context
,
$event
->
getContext
(
)
)
;
static
::
assertEquals
(
AppDeactivatedEvent::NAME,
$event
->
getName
(
)
)
;
static
::
assertEquals
(
[
]
,
$event
->
getWebhookPayload
(
)
)
;
}
public
function
testIsAllowed
(
)
: void
{
$appId
= Uuid::
randomHex
(
)
;
$app
=
(
new
AppEntity
(
)
)
->
assign
(
[
'id' =>
$appId
]
)
;
$context
= Context::
createDefaultContext
(
)
;
$app
=
$this
->
getInstalledApp
(
$this
->context
)
;
static
::
assertNotNull
(
$app
)
;
$shopId
=
$this
->
changeAppUrl
(
)
;
$themeLifecycleHandler
= null;
if
(
class_exists
(
ThemeAppLifecycleHandler::
class
)
)
{
$themeLifecycleHandler
=
$this
->
createMock
(
ThemeAppLifecycleHandler::
class
)
;
$themeLifecycleHandler
->
expects
(
static
::
once
(
)
)
->
method
(
'handleUninstall'
)
->
with
(
static
::
callback
(
fn
(
AppDeactivatedEvent
$event
)
=>
$event
->
getApp
(
)
->
getName
(
)
===
$app
->
getName
(
)
)
)
;
}
$uninstallAppsResolver
=
new
UninstallAppsStrategy
(
$this
->
getContainer
(
)
->
get
(
'app.repository'
)
,
$this
->systemConfigService,
$themeLifecycleHandler
)
;
$uninstallAppsResolver
->
resolve
(
$this
->context
)
;
public
function
filterForEvent
(
string
$name
)
{
return
$this
->
filterByProperty
(
'eventName',
$name
)
;
}
/** * @return array<string> */
public
function
getAclRoleIdsAsBinary
(
)
: array
{
return
array_values
(
$this
->
fmap
(
static
function
DWebhookEntity
$webhook
)
: ?string
{
if
(
$webhook
->
getApp
(
)
)
{
return
Uuid::
fromHexToBytes
(
$webhook
->
getApp
(
)
->
getAclRoleId
(
)
)
;
}
return
null;
}
)
)
;
}
protected
function
getExpectedClass
(
)
: string
{
return
WebhookEntity::
class
;
}
}
public
function
getName
(
)
: string
{
return
self::HOOK_NAME;
}
/** * @internal */
public
function
getAppId
(
)
: string
{
return
$this
->event->
getApp
(
)
->
getId
(
)
;
}
}
return
$this
->
formatCollection
(
$actionButtons
)
;
}
/** * @return array<int, array<string, array<string, string|null>|string|null>> */
private
function
formatCollection
(
ActionButtonCollection
$actionButtons
)
: array
{
return
array_values
(
array_map
(
function
DActionButtonEntity
$button
)
: array
{
/** @var AppEntity $app */
$app
=
$button
->
getApp
(
)
;
return
[
'app' =>
$app
->
getName
(
)
,
'id' =>
$button
->
getId
(
)
,
'label' =>
$this
->
mapTranslatedLabels
(
$button
)
,
'action' =>
$button
->
getAction
(
)
,
'url' =>
$button
->
getUrl
(
)
,
'icon' =>
$app
->
getIcon
(
)
,
]
;
}
,
$actionButtons
->
getElements
(
)
)
)
;
}
public
static
function
getSubscribedEvents
(
)
: array
{
return
[
AppUpdatedEvent::
class
=>
'handleAppActivationOrUpdate',
AppActivatedEvent::
class
=>
'handleAppActivationOrUpdate',
AppDeactivatedEvent::
class
=>
'handleUninstall',
]
;
}
public
function
handleAppActivationOrUpdate
(
AppChangedEvent
$event
)
: void
{
$app
=
$event
->
getApp
(
)
;
if
(
!
$app
->
isActive
(
)
)
{
return
;
}
$configurationCollection
=
$this
->themeRegistry->
getConfigurations
(
)
;
$config
=
$configurationCollection
->
getByTechnicalName
(
$app
->
getName
(
)
)
;
if
(
!
$config
)
{
$config
=
$this
->themeConfigFactory->
createFromApp
(
$app
->
getName
(
)
,
$app
->
getPath
(
)
)
;
$configurationCollection
=
clone
$configurationCollection
;
$configurationCollection
->
add
(
$config
)
;
}
public
function
getName
(
)
: string
{
return
self::HOOK_NAME;
}
/** * @internal */
public
function
getAppId
(
)
: string
{
return
$this
->event->
getApp
(
)
->
getId
(
)
;
}
}
if
(
!
$paymentHandler
)
{
throw
PaymentException::
unknownPaymentMethod
(
$paymentMethod
->
getId
(
)
)
;
}
return
$paymentHandler
;
}
private
function
getPaymentHandlerFromSalesChannelContext
(
SalesChannelContext
$salesChannelContext
)
: ?PaymentHandlerInterface
{
$paymentMethod
=
$salesChannelContext
->
getPaymentMethod
(
)
;
if
(
(
$appPaymentMethod
=
$paymentMethod
->
getAppPaymentMethod
(
)
)
&&
$appPaymentMethod
->
getApp
(
)
)
{
return
$this
->paymentHandlerRegistry->
getPaymentMethodHandler
(
$paymentMethod
->
getId
(
)
)
;
}
$criteria
=
new
Criteria
(
)
;
$criteria
->
setTitle
(
'prepared-payment-handler'
)
;
$criteria
->
addAssociation
(
'app'
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'paymentMethodId',
$paymentMethod
->
getId
(
)
)
)
;
/** @var AppPaymentMethodEntity $appPaymentMethod */
$appPaymentMethod
=
$this
->appPaymentMethodRepository->
search
(
$criteria
,
$salesChannelContext
->
getContext
(
)
)
->
first
(
)
;
$paymentMethod
->
setAppPaymentMethod
(
$appPaymentMethod
)
;
$request
=
$this
->
getLastRequest
(
)
;
static
::
assertNotNull
(
$request
)
;
static
::
assertEquals
(
'POST',
$request
->
getMethod
(
)
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
static
::
assertNotFalse
(
$body
)
;
static
::
assertJson
(
$body
)
;
$data
= \
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
$shopIdProvider
=
$this
->
getContainer
(
)
->
get
(
ShopIdProvider::
class
)
;
$app
=
$action
->
getApp
(
)
;
static
::
assertNotNull
(
$app
)
;
$expectedSource
=
[
'url' =>
getenv
(
'APP_URL'
)
,
'appVersion' =>
$app
->
getVersion
(
)
,
'shopId' =>
$shopIdProvider
->
getShopId
(
)
,
]
;
$expectedData
=
[
'ids' =>
$ids
,
'action' =>
$action
->
getAction
(
)
,
'entity' =>
$action
->
getEntity
(
)
,
]
;
class
AppSyncPaymentHandler
extends
AppPaymentHandler
implements
SynchronousPaymentHandlerInterface
{
public
function
pay
(
SyncPaymentTransactionStruct
$transaction
, RequestDataBag
$dataBag
, SalesChannelContext
$salesChannelContext
)
: void
{
$payUrl
=
$this
->
getAppPaymentMethod
(
$transaction
->
getOrderTransaction
(
)
)
->
getPayUrl
(
)
;
if
(
empty
(
$payUrl
)
)
{
return
;
}
$payload
=
$this
->
buildPayload
(
$transaction
,
$dataBag
->
all
(
)
)
;
$app
=
$this
->
getAppPaymentMethod
(
$transaction
->
getOrderTransaction
(
)
)
->
getApp
(
)
;
if
(
$app
=== null
)
{
throw
PaymentException::
syncProcessInterrupted
(
$transaction
->
getOrderTransaction
(
)
->
getId
(
)
, 'App not defined'
)
;
}
try
{
$response
=
$this
->payloadService->
request
(
$payUrl
,
$payload
,
$app
, SyncPayResponse::
class
,
$salesChannelContext
->
getContext
(
)
)
;
}
catch
(
ClientExceptionInterface
$exception
)
{
throw
PaymentException::
asyncProcessInterrupted
(
$transaction
->
getOrderTransaction
(
)
->
getId
(
)
,
sprintf
(
'App error: %s',
$exception
->
getMessage
(
)
)
)
;
}
if
(
!
$response
instanceof SyncPayResponse
)
{
if
(
$actionButton
=== null
)
{
throw
new
ActionNotFoundException
(
)
;
}
try
{
$shopId
=
$this
->shopIdProvider->
getShopId
(
)
;
}
catch
(
AppUrlChangeDetectedException
)
{
throw
new
ActionNotFoundException
(
)
;
}
/** @var string $secret */
$secret
=
$actionButton
->
getApp
(
)
->
getAppSecret
(
)
;
return
new
AppAction
(
$actionButton
->
getUrl
(
)
,
$this
->url,
$actionButton
->
getApp
(
)
->
getVersion
(
)
,
$actionButton
->
getEntity
(
)
,
$actionButton
->
getAction
(
)
,
$ids
,
$secret
,
$shopId
,
$actionId
)
;
public
function
getName
(
)
: string
{
return
self::HOOK_NAME;
}
/** * @internal */
public
function
getAppId
(
)
: string
{
return
$this
->event->
getApp
(
)
->
getId
(
)
;
}
}
public
function
getName
(
)
: string
{
return
self::HOOK_NAME;
}
/** * @internal */
public
function
getAppId
(
)
: string
{
return
$this
->event->
getApp
(
)
->
getId
(
)
;
}
}
$this
->
loadAppsFromDir
(
__DIR__ . '/../../Theme/fixtures/Apps/theme'
)
;
$context
= Context::
createDefaultContext
(
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'name', 'SwagTheme'
)
)
;
/** @var string $appId */
$appId
=
$this
->appRepo->
searchIds
(
$criteria
,
$context
)
->
firstId
(
)
;
$eventWasReceived
= false;
$onAppDeactivation
=
function
DAppDeactivatedEvent
$event
)
use
(
&
$eventWasReceived
,
$appId
,
$context
)
: void
{
$eventWasReceived
= true;
static
::
assertEquals
(
$appId
,
$event
->
getApp
(
)
->
getId
(
)
)
;
static
::
assertEquals
(
$context
,
$event
->
getContext
(
)
)
;
}
;
$this
->eventDispatcher->
addListener
(
AppDeactivatedEvent::
class
,
$onAppDeactivation
)
;
$this
->appStateService->
deactivateApp
(
$appId
,
$context
)
;
static
::
assertTrue
(
$eventWasReceived
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'appId',
$appId
)
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'active', true
)
)
;
class
AppActivatedEventTest
extends
TestCase
{
public
function
testGetter
(
)
: void
{
$app
=
new
AppEntity
(
)
;
$context
= Context::
createDefaultContext
(
)
;
$event
=
new
AppActivatedEvent
(
$app
,
$context
)
;
static
::
assertEquals
(
$app
,
$event
->
getApp
(
)
)
;
static
::
assertEquals
(
$context
,
$event
->
getContext
(
)
)
;
static
::
assertEquals
(
AppActivatedEvent::NAME,
$event
->
getName
(
)
)
;
static
::
assertEquals
(
[
]
,
$event
->
getWebhookPayload
(
)
)
;
}
public
function
testIsAllowed
(
)
: void
{
$appId
= Uuid::
randomHex
(
)
;
$app
=
(
new
AppEntity
(
)
)
->
assign
(
[
'id' =>
$appId
]
)
;
$context
= Context::
createDefaultContext
(
)
;