1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Model\Order\AutoProcessing;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory;
use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
use Temando\Shipping\Model\Shipment\ShipmentErrorInterface;
/**
* Temando Order Fulfillment Comments History Updater.
*
* @package Temando\Shipping\Model
* @author Christoph Aßmann <christoph.assmann@netresearch.de>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.temando.com/
*/
class OrderStatusHistoryUpdater
{
/**
* @var OrderStatusHistoryInterfaceFactory
*/
private $historyFactory;
/**
* @var OrderStatusHistoryRepositoryInterface
*/
private $historyRepository;
/**
* OrderStatusHistoryUpdater constructor.
* @param OrderStatusHistoryInterfaceFactory $historyFactory
* @param OrderStatusHistoryRepositoryInterface $historyRepository
*/
public function __construct(
OrderStatusHistoryInterfaceFactory $historyFactory,
OrderStatusHistoryRepositoryInterface $historyRepository
) {
$this->historyFactory = $historyFactory;
$this->historyRepository = $historyRepository;
}
/**
* @param OrderInterface $order
* @param string $comment
* @return bool
*/
public function addComment(OrderInterface $order, $comment)
{
$historyItem = $this->historyFactory->create(['data' => [
OrderStatusHistoryInterface::COMMENT => $comment,
OrderStatusHistoryInterface::PARENT_ID => $order->getEntityId(),
OrderStatusHistoryInterface::ENTITY_NAME => 'order',
OrderStatusHistoryInterface::STATUS => $order->getStatus(),
OrderStatusHistoryInterface::IS_CUSTOMER_NOTIFIED => false,
]]);
try {
$this->historyRepository->save($historyItem);
return true;
} catch (CouldNotSaveException $exception) {
return false;
}
}
/**
* @param OrderInterface $order
* @param ShipmentErrorInterface[] $errors
* @return void
*/
public function addErrors(OrderInterface $order, array $errors)
{
foreach ($errors as $error) {
$comment = $error->getDetail()
? sprintf('%s - %s', $error->getTitle(), $error->getDetail())
: $error->getTitle();
$this->addComment($order, $comment);
}
}
}