Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdatingService.php
Go to the documentation of this file.
1 <?php
7 
16 
21 {
25  private $messageGenerator;
26 
30  private $caseRepository;
31 
35  private $commentsHistoryUpdater;
36 
40  private $orderGridUpdater;
41 
45  private $orderStateService;
46 
56  public function __construct(
57  GeneratorInterface $messageGenerator,
58  CaseRepositoryInterface $caseRepository,
59  CommentsHistoryUpdater $commentsHistoryUpdater,
60  OrderGridUpdater $orderGridUpdater,
61  OrderStateService $orderStateService
62  ) {
63  $this->messageGenerator = $messageGenerator;
64  $this->caseRepository = $caseRepository;
65  $this->commentsHistoryUpdater = $commentsHistoryUpdater;
66  $this->orderGridUpdater = $orderGridUpdater;
67  $this->orderStateService = $orderStateService;
68  }
69 
79  public function update(CaseInterface $case, array $data)
80  {
81  if (empty($case->getEntityId()) || empty($case->getCaseId())) {
82  throw new LocalizedException(__('The case entity should not be empty.'));
83  }
84 
85  try {
86  $previousDisposition = $case->getGuaranteeDisposition();
87  $this->setCaseData($case, $data);
88  $orderHistoryComment = $this->messageGenerator->generate($data);
89  $case = $this->caseRepository->save($case);
90  $this->orderGridUpdater->update($case->getOrderId());
91  $this->commentsHistoryUpdater->addComment($case, $orderHistoryComment);
92  if ($case->getGuaranteeDisposition() !== $previousDisposition) {
93  $this->orderStateService->updateByCase($case);
94  }
95  } catch (\Exception $e) {
96  throw new LocalizedException(__('Cannot update Case entity.'), $e);
97  }
98  }
99 
107  private function setCaseData(CaseInterface $case, array $data)
108  {
109  // list of keys which should not be replaced, like order id
110  $notResolvedKeys = [
111  'orderId'
112  ];
113  foreach ($data as $key => $value) {
114  $methodName = 'set' . ucfirst($key);
115  if (!in_array($key, $notResolvedKeys) && method_exists($case, $methodName)) {
116  call_user_func([$case, $methodName], $value);
117  }
118  }
119  }
120 }
$case
__()
Definition: __.php:13
$caseRepository
$value
Definition: gender.phtml:16
__construct(GeneratorInterface $messageGenerator, CaseRepositoryInterface $caseRepository, CommentsHistoryUpdater $commentsHistoryUpdater, OrderGridUpdater $orderGridUpdater, OrderStateService $orderStateService)