From 17f0ce276fb962e402b3031df98e0887f6e8de9f Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Mon, 25 Feb 2019 15:27:44 +0100 Subject: [PATCH 1/4] [OrderBundle, WorkflowBundle] refactor state change logging for workflows -> move classes to workflow bundle --- .../Migrations/Version20190225140315.php | 27 ++++ .../Controller/OrderController.php | 5 +- .../Operator/Factory/OrderStateFactory.php | 8 +- .../GridColumnConfig/Operator/OrderState.php | 8 +- .../Resources/config/services/grid_config.yml | 2 +- .../Resources/config/services/order.yml | 4 +- .../Resources/config/services/workflow.yml | 16 +- .../Resources/translations/admin.de.yml | 1 - .../Resources/translations/admin.en.yml | 1 - .../Templating/Helper/OrderStateHelper.php | 8 +- .../Workflow/OrderHistoryLogger.php | 58 +++---- .../Workflow/OrderStateHistoryLogger.php | 103 +++--------- .../Workflow/WorkflowStateManager.php | 150 +++++------------- .../WorkflowBundle/History/HistoryLogger.php | 76 +++++++++ .../History/HistoryLoggerInterface.php | 26 +++ .../History/HistoryRepository.php | 46 ++++++ .../History/HistoryRepositoryInterface.php | 23 +++ .../History/StateHistoryLogger.php | 105 ++++++++++++ .../History/StateHistoryLoggerInterface.php | 25 +++ src/CoreShop/Bundle/WorkflowBundle/Notes.php | 21 +++ .../Resources/config/services/workflow.yml | 34 ++++ .../config/translations/admin.de.yml | 2 + .../config/translations/admin.en.yml | 1 + .../StateManager/WorkflowStateInfoManager.php | 145 +++++++++++++++++ .../WorkflowStateInfoManagerInterface.php | 44 +++++ 25 files changed, 678 insertions(+), 261 deletions(-) create mode 100644 src/CoreShop/Bundle/CoreBundle/Migrations/Version20190225140315.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/HistoryLogger.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/HistoryLoggerInterface.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepository.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/StateHistoryLogger.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/History/StateHistoryLoggerInterface.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/Notes.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/Resources/config/translations/admin.de.yml create mode 100644 src/CoreShop/Bundle/WorkflowBundle/Resources/config/translations/admin.en.yml create mode 100644 src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManager.php create mode 100644 src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php diff --git a/src/CoreShop/Bundle/CoreBundle/Migrations/Version20190225140315.php b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20190225140315.php new file mode 100644 index 0000000000..af969576d1 --- /dev/null +++ b/src/CoreShop/Bundle/CoreBundle/Migrations/Version20190225140315.php @@ -0,0 +1,27 @@ +addSql('UPDATE notes SET type="history_state_change" WHERE type="order_state_change"'); + + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + // this down() migration is auto-generated, please modify it to your needs + + } +} diff --git a/src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php b/src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php index 94610f142e..cab747bd03 100644 --- a/src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php +++ b/src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php @@ -14,6 +14,7 @@ use Carbon\Carbon; use CoreShop\Bundle\WorkflowBundle\Manager\StateMachineManager; +use CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface; use CoreShop\Component\Order\Model\OrderInterface; use CoreShop\Component\Order\Model\SaleInterface; use CoreShop\Component\Order\OrderStates; @@ -399,11 +400,11 @@ private function getOrderShipmentRepository() } /** - * @return WorkflowStateManagerInterface + * @return WorkflowStateInfoManagerInterface */ private function getWorkflowStateManager() { - return $this->get('coreshop.workflow.state_manager'); + return $this->get('coreshop.workflow.state_info_manager'); } /** diff --git a/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/Factory/OrderStateFactory.php b/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/Factory/OrderStateFactory.php index a86a331083..e56655d0bc 100644 --- a/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/Factory/OrderStateFactory.php +++ b/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/Factory/OrderStateFactory.php @@ -13,21 +13,21 @@ namespace CoreShop\Bundle\OrderBundle\Pimcore\GridColumnConfig\Operator\Factory; use CoreShop\Bundle\OrderBundle\Pimcore\GridColumnConfig\Operator\OrderState; -use CoreShop\Component\Order\Workflow\WorkflowStateManagerInterface; +use CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface; use Pimcore\DataObject\GridColumnConfig\Operator\Factory\OperatorFactoryInterface; use Pimcore\DataObject\GridColumnConfig\Operator\OperatorInterface; class OrderStateFactory implements OperatorFactoryInterface { /** - * @var WorkflowStateManagerInterface + * @var WorkflowStateInfoManagerInterface */ private $workflowManager; /** - * @param WorkflowStateManagerInterface $workflowManager + * @param WorkflowStateInfoManagerInterface $workflowManager */ - public function __construct(WorkflowStateManagerInterface $workflowManager) + public function __construct(WorkflowStateInfoManagerInterface $workflowManager) { $this->workflowManager = $workflowManager; } diff --git a/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/OrderState.php b/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/OrderState.php index 1470ad864d..61dea52b6a 100644 --- a/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/OrderState.php +++ b/src/CoreShop/Bundle/OrderBundle/Pimcore/GridColumnConfig/Operator/OrderState.php @@ -12,7 +12,7 @@ namespace CoreShop\Bundle\OrderBundle\Pimcore\GridColumnConfig\Operator; -use CoreShop\Component\Order\Workflow\WorkflowStateManagerInterface; +use CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface; use Pimcore\DataObject\GridColumnConfig\Operator\AbstractOperator; class OrderState extends AbstractOperator @@ -23,16 +23,16 @@ class OrderState extends AbstractOperator private $highlightLabel = false; /** - * @var WorkflowStateManagerInterface + * @var WorkflowStateInfoManagerInterface */ private $workflowManager; /** - * @param WorkflowStateManagerInterface $workflowManager + * @param WorkflowStateInfoManagerInterface $workflowManager * @param \stdClass $config * @param null $context */ - public function __construct(WorkflowStateManagerInterface $workflowManager, \stdClass $config, $context = null) + public function __construct(WorkflowStateInfoManagerInterface $workflowManager, \stdClass $config, $context = null) { parent::__construct($config, $context); $this->workflowManager = $workflowManager; diff --git a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/grid_config.yml b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/grid_config.yml index 1affda9ebe..7492328807 100644 --- a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/grid_config.yml +++ b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/grid_config.yml @@ -5,7 +5,7 @@ services: coreshop.data_object.grid_column_config.operator.factory.order_state: class: CoreShop\Bundle\OrderBundle\Pimcore\GridColumnConfig\Operator\Factory\OrderStateFactory arguments: - - '@coreshop.workflow.state_manager' + - '@coreshop.workflow.state_info_manager' tags: - { name: pimcore.data_object.grid_column_config.operator_factory, id: coreshop_order_state } diff --git a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/order.yml b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/order.yml index e11b396fac..73500dfbae 100644 --- a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/order.yml +++ b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/order.yml @@ -69,7 +69,7 @@ services: coreshop.templating.helper.order_state: class: CoreShop\Bundle\OrderBundle\Templating\Helper\OrderStateHelper - arguments: ['@coreshop.workflow.state_manager'] + arguments: ['@coreshop.workflow.state_info_manager'] tags: - { name: templating.helper, alias: coreshop_order_state } @@ -77,4 +77,4 @@ services: class: CoreShop\Bundle\OrderBundle\Twig\OrderStateExtension arguments: ['@coreshop.templating.helper.order_state'] tags: - - { name: twig.extension } \ No newline at end of file + - { name: twig.extension } diff --git a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/workflow.yml b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/workflow.yml index f58d2e06cf..e71f6b8b8e 100644 --- a/src/CoreShop/Bundle/OrderBundle/Resources/config/services/workflow.yml +++ b/src/CoreShop/Bundle/OrderBundle/Resources/config/services/workflow.yml @@ -2,7 +2,6 @@ parameters: _defaults: public: true - coreshop.workflow.manager.order.history_log_state_identifier: !php/const CoreShop\Component\Order\Notes::ORDER_HISTORY_STATE_LOG coreshop.workflow.include_invoice_state_to_complete_order: false services: @@ -100,24 +99,15 @@ services: coreshop.workflow.state_manager: class: CoreShop\Bundle\OrderBundle\Workflow\WorkflowStateManager arguments: - - '@event_dispatcher' - - '@coreshop.state_machine_manager' - - '@pimcore.translator' - - '%coreshop.workflow.manager.order.history_log_state_identifier%' - - '%coreshop.state_machine.colors%' + - '@coreshop.workflow.state_info_manager' coreshop.workflow.order_history_logger: class: CoreShop\Bundle\OrderBundle\Workflow\OrderHistoryLogger arguments: - '@coreshop.repository.order' - - '@coreshop.object_note_service' - - '@pimcore.translator' - - '%coreshop.workflow.manager.order.history_log_state_identifier%' + - '@coreshop.workflow.history_logger' coreshop.workflow.order_state_history_logger: class: CoreShop\Bundle\OrderBundle\Workflow\OrderStateHistoryLogger arguments: - - '@coreshop.state_machine_manager' - - '@coreshop.object_note_service' - - '@pimcore.translator' - - '%coreshop.workflow.manager.order.history_log_state_identifier%' + - '@coreshop.workflow.state_history_logger' diff --git a/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.de.yml b/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.de.yml index 040271c49e..c2f371eceb 100644 --- a/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.de.yml +++ b/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.de.yml @@ -209,7 +209,6 @@ coreshop_workflow_transition_coreshop_shipment_cancel: 'Stornieren' coreshop_workflow_transition_coreshop_invoice_create: 'Erstellen' coreshop_workflow_transition_coreshop_invoice_complete: 'Abschließen' coreshop_workflow_transition_coreshop_invoice_cancel: 'Stornieren' -coreshop_workflow_order_history_logger_prefix: 'Log' coreshop_discount: 'Rabatt (inkl. Mwst.)' coreshop_discount_tax: 'Rabatt Mwst.' coreshop_discount_without_tax: 'Rabatt (exkl. Mwst.)' diff --git a/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.en.yml b/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.en.yml index b461f654d9..a80e4e7433 100644 --- a/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.en.yml +++ b/src/CoreShop/Bundle/OrderBundle/Resources/translations/admin.en.yml @@ -208,7 +208,6 @@ coreshop_workflow_transition_coreshop_shipment_cancel: 'Cancel' coreshop_workflow_transition_coreshop_invoice_create: 'Create' coreshop_workflow_transition_coreshop_invoice_complete: 'Complete' coreshop_workflow_transition_coreshop_invoice_cancel: 'Cancel' -coreshop_workflow_order_history_logger_prefix: 'Log' coreshop_discount: 'Discount (incl. Tax)' coreshop_discount_tax: 'Discount Tax' coreshop_discount_without_tax: 'Discount (excl. Tax.)' diff --git a/src/CoreShop/Bundle/OrderBundle/Templating/Helper/OrderStateHelper.php b/src/CoreShop/Bundle/OrderBundle/Templating/Helper/OrderStateHelper.php index a3f71df354..8071cebdbc 100644 --- a/src/CoreShop/Bundle/OrderBundle/Templating/Helper/OrderStateHelper.php +++ b/src/CoreShop/Bundle/OrderBundle/Templating/Helper/OrderStateHelper.php @@ -12,24 +12,24 @@ namespace CoreShop\Bundle\OrderBundle\Templating\Helper; +use CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface; use CoreShop\Component\Order\Model\OrderInterface; use CoreShop\Component\Order\OrderPaymentStates; use CoreShop\Component\Order\OrderShipmentStates; use CoreShop\Component\Order\OrderStates; -use CoreShop\Component\Order\Workflow\WorkflowStateManagerInterface; use Symfony\Component\Templating\Helper\Helper; class OrderStateHelper extends Helper implements OrderStateHelperInterface { /** - * @var WorkflowStateManagerInterface + * @var WorkflowStateInfoManagerInterface */ private $workflowStateManager; /** - * @param WorkflowStateManagerInterface $workflowStateManager + * @param WorkflowStateInfoManagerInterface $workflowStateManager */ - public function __construct(WorkflowStateManagerInterface $workflowStateManager) + public function __construct(WorkflowStateInfoManagerInterface $workflowStateManager) { $this->workflowStateManager = $workflowStateManager; } diff --git a/src/CoreShop/Bundle/OrderBundle/Workflow/OrderHistoryLogger.php b/src/CoreShop/Bundle/OrderBundle/Workflow/OrderHistoryLogger.php index 435462cc3f..7c301e786f 100644 --- a/src/CoreShop/Bundle/OrderBundle/Workflow/OrderHistoryLogger.php +++ b/src/CoreShop/Bundle/OrderBundle/Workflow/OrderHistoryLogger.php @@ -12,11 +12,13 @@ namespace CoreShop\Bundle\OrderBundle\Workflow; +use CoreShop\Bundle\WorkflowBundle\History\HistoryLoggerInterface; use CoreShop\Component\Order\Model\OrderInterface; use CoreShop\Component\Order\Repository\OrderRepositoryInterface; -use CoreShop\Component\Pimcore\DataObject\NoteServiceInterface; -use Symfony\Component\Translation\TranslatorInterface; +/** + * @deprecated Class CoreShop\Bundle\OrderBundle\Workflow\OrderHistoryLogger is deprecated and will be removed with 2.1, use CoreShop\Bundle\WorkflowBundle\History\HistoryLoggerInterface instead + */ final class OrderHistoryLogger { /** @@ -25,36 +27,20 @@ final class OrderHistoryLogger private $orderRepository; /** - * @var NoteServiceInterface - */ - private $noteService; - - /** - * @var TranslatorInterface - */ - private $translator; - - /** - * @var string + * @var HistoryLoggerInterface */ - private $noteIdentifier; + private $historyLogger; /** * @param OrderRepositoryInterface $orderRepository - * @param NoteServiceInterface $noteService - * @param TranslatorInterface $translator - * @param string $noteIdentifier + * @param HistoryLoggerInterface $historyLogger */ public function __construct( OrderRepositoryInterface $orderRepository, - NoteServiceInterface $noteService, - TranslatorInterface $translator, - $noteIdentifier + HistoryLoggerInterface $historyLogger ) { $this->orderRepository = $orderRepository; - $this->noteService = $noteService; - $this->translator = $translator; - $this->noteIdentifier = $noteIdentifier; + $this->historyLogger = $historyLogger; } /** @@ -65,25 +51,21 @@ public function __construct( */ public function log($orderId = null, $message = null, $description = null, $translate = false) { + trigger_error( + sprintf('%s::%s is deprecated and will be removed with 2.1, please use %s:%s instead.', + static::class, + __METHOD__, + HistoryLoggerInterface::class, + __METHOD__ + ), + E_USER_DEPRECATED + ); + $order = $this->orderRepository->find($orderId); if (!$order instanceof OrderInterface) { return; } - $note = $this->noteService->createPimcoreNoteInstance($order, $this->noteIdentifier); - - $message = strip_tags($message); - - if ($translate === true) { - $message = $this->translator->trans($message, [], 'admin'); - } - - $note->setTitle($this->translator->trans('coreshop_workflow_order_history_logger_prefix', [], 'admin') . ': ' . $message); - - if (!is_null($description)) { - $note->setDescription($description); - } - - $this->noteService->storeNote($note); + $this->historyLogger->log($order, $message, $description, $translate); } } diff --git a/src/CoreShop/Bundle/OrderBundle/Workflow/OrderStateHistoryLogger.php b/src/CoreShop/Bundle/OrderBundle/Workflow/OrderStateHistoryLogger.php index 5dc8b55f9e..1603d82861 100644 --- a/src/CoreShop/Bundle/OrderBundle/Workflow/OrderStateHistoryLogger.php +++ b/src/CoreShop/Bundle/OrderBundle/Workflow/OrderStateHistoryLogger.php @@ -12,50 +12,26 @@ namespace CoreShop\Bundle\OrderBundle\Workflow; +use CoreShop\Bundle\WorkflowBundle\History\StateHistoryLoggerInterface; use CoreShop\Component\Order\Model\OrderInterface; -use CoreShop\Bundle\WorkflowBundle\Manager\StateMachineManager; -use CoreShop\Component\Pimcore\DataObject\NoteServiceInterface; -use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Workflow\Event\Event; +/** + * @deprecated Class CoreShop\Bundle\OrderBundle\Workflow\OrderStateHistoryLogger is deprecated and will be removed with 2.1, use CoreShop\Bundle\WorkflowBundle\History\StateHistoryLoggerInterface instead + */ final class OrderStateHistoryLogger { /** - * @var StateMachineManager - */ - private $stateMachineManager; - - /** - * @var NoteServiceInterface - */ - private $noteService; - - /** - * @var TranslatorInterface - */ - private $translator; - - /** - * @var string + * @var StateHistoryLoggerInterface */ - private $noteIdentifier; + private $stateHistoryLogger; /** - * @param StateMachineManager $stateMachineManager - * @param NoteServiceInterface $noteService - * @param TranslatorInterface $translator - * @param string $noteIdentifier + * @param StateHistoryLoggerInterface $stateHistoryLogger */ - public function __construct( - StateMachineManager $stateMachineManager, - NoteServiceInterface $noteService, - TranslatorInterface $translator, - $noteIdentifier - ) { - $this->stateMachineManager = $stateMachineManager; - $this->noteService = $noteService; - $this->translator = $translator; - $this->noteIdentifier = $noteIdentifier; + public function __construct(StateHistoryLoggerInterface $stateHistoryLogger) + { + $this->stateHistoryLogger = $stateHistoryLogger; } /** @@ -64,57 +40,16 @@ public function __construct( */ public function log(OrderInterface $order, Event $event) { - $subject = $event->getSubject(); - $transition = $event->getTransition(); - - $from = $this->getFrom($transition->getFroms()); - $to = $this->getTo($transition->getTos()); - - $fromValue = 'coreshop_workflow_state_' . $event->getWorkflowName() . '_' . $from; - $toValue = 'coreshop_workflow_state_' . $event->getWorkflowName() . '_' . $to; - - $objectIdInfo = ''; - // add id if it's not an order (since payment/shipping/invoice could be more than one) - if (!$subject instanceof OrderInterface) { - $objectIdInfo = ' (Id ' . $subject->getId() . ')'; - } - - $note = $this->noteService->createPimcoreNoteInstance($order, $this->noteIdentifier); - $note->setTitle( - sprintf( - '%s%s: %s %s %s %s', - $this->translator->trans('coreshop_workflow_name_' . $event->getWorkflowName(), [], 'admin'), - $objectIdInfo, - $this->translator->trans('coreshop_workflow_state_changed_from', [], 'admin'), - $this->translator->trans($fromValue, [], 'admin'), - $this->translator->trans('coreshop_workflow_state_changed_to', [], 'admin'), - $this->translator->trans($toValue, [], 'admin') - ) + trigger_error( + sprintf('%s::%s is deprecated and will be removed with 2.1, please use %s:%s instead.', + static::class, + __METHOD__, + StateHistoryLoggerInterface::class, + __METHOD__ + ), + E_USER_DEPRECATED ); - $note->addData('workflow', 'text', $event->getWorkflowName()); - $note->addData('transition', 'text', $transition->getName()); - - $this->noteService->storeNote($note); - } - - /** - * @param array $froms - * - * @return mixed - */ - private function getFrom(array $froms) - { - return reset($froms); - } - - /** - * @param array $tos - * - * @return mixed - */ - private function getTo(array $tos) - { - return reset($tos); + $this->stateHistoryLogger->log($order, $event); } } diff --git a/src/CoreShop/Bundle/OrderBundle/Workflow/WorkflowStateManager.php b/src/CoreShop/Bundle/OrderBundle/Workflow/WorkflowStateManager.php index c1c2cd3b12..91fc405b67 100644 --- a/src/CoreShop/Bundle/OrderBundle/Workflow/WorkflowStateManager.php +++ b/src/CoreShop/Bundle/OrderBundle/Workflow/WorkflowStateManager.php @@ -12,129 +12,62 @@ namespace CoreShop\Bundle\OrderBundle\Workflow; -use CoreShop\Bundle\OrderBundle\Event\WorkflowTransitionEvent; -use CoreShop\Bundle\WorkflowBundle\Manager\StateMachineManager; use CoreShop\Component\Order\Model\ProposalInterface; use CoreShop\Component\Order\Workflow\WorkflowStateManagerInterface; -use Pimcore\Model\Element\Note; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Translation\TranslatorInterface; +/** + * @deprecated CoreShop\Bundle\OrderBundle\Workflow\WorkflowStateManager is deprecated and will be removed with 2.1, please use \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface instead + */ final class WorkflowStateManager implements WorkflowStateManagerInterface { /** - * @var EventDispatcherInterface - */ - private $eventDispatcher; - - /** - * @var StateMachineManager - */ - private $stateMachineManager; - - /** - * @var TranslatorInterface + * @var \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface */ - private $translator; + private $stateManager; /** - * @var string - */ - private $noteIdentifier; - - /** - * @var array - */ - private $stateColors; - - /** - * @param EventDispatcherInterface $eventDispatcher - * @param StateMachineManager $stateMachineManager - * @param TranslatorInterface $translator - * @param string $noteIdentifier - * @param array $stateColors + * @param \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface $stateManager */ public function __construct( - EventDispatcherInterface $eventDispatcher, - StateMachineManager $stateMachineManager, - TranslatorInterface $translator, - $noteIdentifier, - $stateColors + \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface $stateManager ) { - $this->eventDispatcher = $eventDispatcher; - $this->stateMachineManager = $stateMachineManager; - $this->noteIdentifier = $noteIdentifier; - $this->translator = $translator; - $this->stateColors = $stateColors; + $this->stateManager = $stateManager; } /** - * @param string $workflowName - * @param mixed $value - * @param bool $forFrontend - * - * @return array + * {@inheritdoc} */ public function getStateInfo($workflowName, $value, $forFrontend = true) { - $transPrefix = $forFrontend ? 'coreshop.ui.workflow.state.' : 'coreshop_workflow_state_'; - $transValue = $transPrefix . $workflowName . ($forFrontend ? '.' : '_') . $value; - - $color = isset($this->stateColors[$workflowName]['place_colors'][$value]) ? $this->stateColors[$workflowName]['place_colors'][$value] : '#f6f1de'; - - $data = [ - 'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'), - 'state' => $value, - 'color' => $color, - ]; - - return $data; - } - - /** - * @param string $workflowName - * @param string $transition - * @param bool $forFrontend - * - * @return array - */ - public function getTransitionInfo($workflowName, $transition, $forFrontend = true) - { - $transPrefix = $forFrontend ? 'coreshop.ui.workflow.transition.' : 'coreshop_workflow_transition_'; - $transValue = $transPrefix . $workflowName . ($forFrontend ? '.' : '_') . $transition; - $color = isset($this->stateColors[$workflowName]['transition_colors'][$transition]) ? $this->stateColors[$workflowName]['transition_colors'][$transition] : '#999999'; - - $data = [ - 'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'), - 'transition' => $transition, - 'color' => $color, - ]; - - return $data; + trigger_error( + sprintf('%s::%s is deprecated and will be removed with 2.1, please use %s:%s instead.', + static::class, + __METHOD__, + \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface::class, + __METHOD__ + ), + E_USER_DEPRECATED + ); + + return $this->stateManager->getStateInfo($workflowName, $value, $forFrontend); } /** - * @param string $subject - * @param string $workflowName - * @param array $transitions - * @param bool $forFrontend - * - * @return array + * {@inheritdoc} */ public function parseTransitions($subject, $workflowName, $transitions = [], $forFrontend = true) { - $event = new WorkflowTransitionEvent($transitions, $workflowName); - $this->eventDispatcher->dispatch('coreshop.workflow.valid_transitions', $event); - - $valid = []; - $workflow = $this->stateMachineManager->get($subject, $workflowName); - foreach ($event->getAllowedTransitions() as $transition) { - if ($workflow->can($subject, $transition)) { - $valid[] = $this->getTransitionInfo($workflowName, $transition, $forFrontend); - } - } - - return $valid; + trigger_error( + sprintf('%s::%s is deprecated and will be removed with 2.1, please use %s:%s instead.', + static::class, + __METHOD__, + \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface::class, + __METHOD__ + ), + E_USER_DEPRECATED + ); + + return $this->stateManager->parseTransitions($subject, $workflowName, $transitions, $forFrontend); } /** @@ -142,13 +75,16 @@ public function parseTransitions($subject, $workflowName, $transitions = [], $fo */ public function getStateHistory(ProposalInterface $proposal) { - /* @var \Pimcore\Model\Element\Note\Listing $noteList */ - $noteList = new Note\Listing(); - $noteList->addConditionParam('type = ?', $this->noteIdentifier); - $noteList->addConditionParam('cid = ?', $proposal->getId()); - $noteList->setOrderKey('date'); - $noteList->setOrder('desc'); - - return $noteList->load(); + trigger_error( + sprintf('%s::%s is deprecated and will be removed with 2.1, please use %s:%s instead.', + static::class, + __METHOD__, + \CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface::class, + __METHOD__ + ), + E_USER_DEPRECATED + ); + + return $this->stateManager->getStateHistory($proposal); } } diff --git a/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLogger.php b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLogger.php new file mode 100644 index 0000000000..4fe48fcac2 --- /dev/null +++ b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLogger.php @@ -0,0 +1,76 @@ +noteService = $noteService; + $this->translator = $translator; + $this->noteIdentifier = $noteIdentifier; + } + + /** + * {@inheritdoc} + */ + public function log(DataObject\Concrete $dataObject, $message = null, $description = null, $translate = false) + { + $note = $this->noteService->createPimcoreNoteInstance($dataObject, $this->noteIdentifier); + + $message = strip_tags($message); + + if ($translate === true) { + $message = $this->translator->trans($message, [], 'admin'); + } + + $note->setTitle( + sprintf('%s: %s', + $this->translator->trans('coreshop_workflow_history_logger_prefix', [], 'admin'), + $message) + ); + + if (null !== $description) { + $note->setDescription($description); + } + + $this->noteService->storeNote($note); + } +} diff --git a/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLoggerInterface.php b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLoggerInterface.php new file mode 100644 index 0000000000..2cb0bd2f41 --- /dev/null +++ b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryLoggerInterface.php @@ -0,0 +1,26 @@ +noteIdentifier = $noteIdentifier; + } + + /** + * {@inheritdoc} + */ + public function getHistory(DataObject\Concrete $object) + { + $noteList = new Note\Listing(); + $noteList->addConditionParam('type = ?', $this->noteIdentifier); + $noteList->addConditionParam('cid = ?', $object->getId()); + $noteList->setOrderKey('date'); + $noteList->setOrder('desc'); + + return $noteList->load(); + } +} diff --git a/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php new file mode 100644 index 0000000000..5869944f61 --- /dev/null +++ b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php @@ -0,0 +1,23 @@ +noteService = $noteService; + $this->translator = $translator; + $this->noteIdentifier = $noteIdentifier; + } + + /** + * {@inheritdoc} + */ + public function log(Concrete $object, Event $event) + { + $transition = $event->getTransition(); + + $from = $this->getFrom($transition->getFroms()); + $to = $this->getTo($transition->getTos()); + + $fromValue = 'coreshop_workflow_state_' . $event->getWorkflowName() . '_' . $from; + $toValue = 'coreshop_workflow_state_' . $event->getWorkflowName() . '_' . $to; + + $objectIdInfo = ' (Id ' . $object->getId() . ')'; + + $note = $this->noteService->createPimcoreNoteInstance($object, $this->noteIdentifier); + $note->setTitle( + sprintf( + '%s%s: %s %s %s %s', + $this->translator->trans('coreshop_workflow_name_' . $event->getWorkflowName(), [], 'admin'), + $objectIdInfo, + $this->translator->trans('coreshop_workflow_state_changed_from', [], 'admin'), + $this->translator->trans($fromValue, [], 'admin'), + $this->translator->trans('coreshop_workflow_state_changed_to', [], 'admin'), + $this->translator->trans($toValue, [], 'admin') + ) + ); + + $note->addData('workflow', 'text', $event->getWorkflowName()); + $note->addData('transition', 'text', $transition->getName()); + + $this->noteService->storeNote($note); + } + + /** + * @param array $froms + * + * @return mixed + */ + private function getFrom(array $froms) + { + return reset($froms); + } + + /** + * @param array $tos + * + * @return mixed + */ + private function getTo(array $tos) + { + return reset($tos); + } +} diff --git a/src/CoreShop/Bundle/WorkflowBundle/History/StateHistoryLoggerInterface.php b/src/CoreShop/Bundle/WorkflowBundle/History/StateHistoryLoggerInterface.php new file mode 100644 index 0000000000..7ff3211659 --- /dev/null +++ b/src/CoreShop/Bundle/WorkflowBundle/History/StateHistoryLoggerInterface.php @@ -0,0 +1,25 @@ +eventDispatcher = $eventDispatcher; + $this->stateMachineManager = $stateMachineManager; + $this->historyRepository = $historyRepository; + $this->translator = $translator; + $this->stateColors = $stateColors; + } + + /** + * @param string $workflowName + * @param mixed $value + * @param bool $forFrontend + * + * @return array + */ + public function getStateInfo($workflowName, $value, $forFrontend = true) + { + $transPrefix = $forFrontend ? 'coreshop.ui.workflow.state.' : 'coreshop_workflow_state_'; + $transValue = $transPrefix.$workflowName.($forFrontend ? '.' : '_').$value; + + $color = isset($this->stateColors[$workflowName]['place_colors'][$value]) ? $this->stateColors[$workflowName]['place_colors'][$value] : '#f6f1de'; + + $data = [ + 'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'), + 'state' => $value, + 'color' => $color, + ]; + + return $data; + } + + /** + * @param string $workflowName + * @param string $transition + * @param bool $forFrontend + * + * @return array + */ + public function getTransitionInfo($workflowName, $transition, $forFrontend = true) + { + $transPrefix = $forFrontend ? 'coreshop.ui.workflow.transition.' : 'coreshop_workflow_transition_'; + $transValue = $transPrefix.$workflowName.($forFrontend ? '.' : '_').$transition; + $color = isset($this->stateColors[$workflowName]['transition_colors'][$transition]) ? $this->stateColors[$workflowName]['transition_colors'][$transition] : '#999999'; + + $data = [ + 'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'), + 'transition' => $transition, + 'color' => $color, + ]; + + return $data; + } + + /** + * @param string $subject + * @param string $workflowName + * @param array $transitions + * @param bool $forFrontend + * + * @return array + */ + public function parseTransitions($subject, $workflowName, $transitions = [], $forFrontend = true) + { + $event = new WorkflowTransitionEvent($transitions, $workflowName); + $this->eventDispatcher->dispatch('coreshop.workflow.valid_transitions', $event); + + $valid = []; + $workflow = $this->stateMachineManager->get($subject, $workflowName); + foreach ($event->getAllowedTransitions() as $transition) { + if ($workflow->can($subject, $transition)) { + $valid[] = $this->getTransitionInfo($workflowName, $transition, $forFrontend); + } + } + + return $valid; + } + + /** + * {@inheritdoc} + */ + public function getStateHistory($proposal) + { + return $this->historyRepository->getHistory($proposal); + } +} diff --git a/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php new file mode 100644 index 0000000000..e9a06935a1 --- /dev/null +++ b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php @@ -0,0 +1,44 @@ + Date: Tue, 26 Feb 2019 11:59:08 +0100 Subject: [PATCH 2/4] [WorkflowBundle] fix doc-types --- .../WorkflowBundle/History/HistoryRepositoryInterface.php | 2 ++ .../StateManager/WorkflowStateInfoManagerInterface.php | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php index 5869944f61..f166d89df6 100644 --- a/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php +++ b/src/CoreShop/Bundle/WorkflowBundle/History/HistoryRepositoryInterface.php @@ -13,11 +13,13 @@ namespace CoreShop\Bundle\WorkflowBundle\History; use Pimcore\Model\DataObject; +use Pimcore\Model\Element\Note; interface HistoryRepositoryInterface { /** * @param DataObject\Concrete $object + * @return Note[] */ public function getHistory(DataObject\Concrete $object); } diff --git a/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php index e9a06935a1..31b04b3a3e 100644 --- a/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php +++ b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php @@ -12,16 +12,16 @@ namespace CoreShop\Bundle\WorkflowBundle\StateManager; -use CoreShop\Component\Order\Model\ProposalInterface; +use Pimcore\Model\Element\Note; interface WorkflowStateInfoManagerInterface { /** - * @param ProposalInterface $proposal + * @param $object * - * @return mixed + * @return Note[] */ - public function getStateHistory($proposal); + public function getStateHistory($object); /** * @param string $workflowName From 91d185639a97ebee675a4642c41efe8e5bf9c92b Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 26 Feb 2019 12:05:39 +0100 Subject: [PATCH 3/4] [WorkflowBundle] add deprecation log --- CHANGELOG-2.0.x.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG-2.0.x.md b/CHANGELOG-2.0.x.md index 6b2efacbe5..0ecca6cf75 100644 --- a/CHANGELOG-2.0.x.md +++ b/CHANGELOG-2.0.x.md @@ -1,5 +1,12 @@ # Within V2 +## 2.0.5: + - Deprecations: + - [WorkflowBundle] refactor state change logging (https://github.com/coreshop/CoreShop/pull/835) + - `CoreShop\Bundle\OrderBundle\Workflow\OrderHistoryLogger` has been deprecated, use `CoreShop\Bundle\WorkflowBundle\History\HistoryLoggerInterface` instead + - `CoreShop\Bundle\OrderBundle\Workflow\OrderStateHistoryLogger` has been deprecated, use `CoreShop\Bundle\WorkflowBundle\History\StateHistoryLoggerInterface` instead + - `CoreShop\Bundle\OrderBundle\Workflow\WorkflowStateManager` has been deprecated, use `CoreShop\Bundle\WorkflowBundle\StateManager\WorkflowStateInfoManagerInterface` instead + ## 2.0.4 - Features: - [Reports] Reports Grid Sorting (https://github.com/coreshop/CoreShop/pull/828) From 76bd592e9d91b3ed05e5a737d9be370859a68250 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Wed, 27 Feb 2019 16:56:52 +0100 Subject: [PATCH 4/4] [WorkflowBundle] make stan happy --- .../StateManager/WorkflowStateInfoManagerInterface.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php index 31b04b3a3e..3e8b352089 100644 --- a/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php +++ b/src/CoreShop/Bundle/WorkflowBundle/StateManager/WorkflowStateInfoManagerInterface.php @@ -12,12 +12,13 @@ namespace CoreShop\Bundle\WorkflowBundle\StateManager; +use Pimcore\Model\DataObject; use Pimcore\Model\Element\Note; interface WorkflowStateInfoManagerInterface { /** - * @param $object + * @param DataObject $object * * @return Note[] */