Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CommentRepository.php
Go to the documentation of this file.
1 <?php
7 
14 use Magento\Sales\Api\Data\ShipmentCommentInterfaceFactory;
15 use Magento\Sales\Api\Data\ShipmentCommentSearchResultInterfaceFactory;
20 use Psr\Log\LoggerInterface;
21 
26 {
30  private $commentResource;
31 
35  private $commentFactory;
36 
40  private $searchResultFactory;
41 
45  private $collectionProcessor;
46 
50  private $shipmentCommentSender;
51 
55  private $shipmentRepository;
56 
60  private $logger;
61 
71  public function __construct(
72  ShipmentCommentResourceInterface $commentResource,
73  ShipmentCommentInterfaceFactory $commentFactory,
74  ShipmentCommentSearchResultInterfaceFactory $searchResultFactory,
75  CollectionProcessorInterface $collectionProcessor,
76  ShipmentCommentSender $shipmentCommentSender = null,
77  ShipmentRepositoryInterface $shipmentRepository = null,
78  LoggerInterface $logger = null
79  ) {
80  $this->commentResource = $commentResource;
81  $this->commentFactory = $commentFactory;
82  $this->searchResultFactory = $searchResultFactory;
83  $this->collectionProcessor = $collectionProcessor;
84  $this->shipmentCommentSender = $shipmentCommentSender
85  ?: ObjectManager::getInstance()->get(ShipmentCommentSender::class);
86  $this->shipmentRepository = $shipmentRepository
87  ?: ObjectManager::getInstance()->get(ShipmentRepositoryInterface::class);
88  $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
89  }
90 
95  {
96  $searchResult = $this->searchResultFactory->create();
97  $this->collectionProcessor->process($searchCriteria, $searchResult);
98  $searchResult->setSearchCriteria($searchCriteria);
99  return $searchResult;
100  }
101 
105  public function get($id)
106  {
107  $entity = $this->commentFactory->create();
108  $this->commentResource->load($entity, $id);
109  return $entity;
110  }
111 
115  public function delete(ShipmentCommentInterface $entity)
116  {
117  try {
118  $this->commentResource->delete($entity);
119  } catch (\Exception $e) {
120  throw new CouldNotDeleteException(__('Could not delete the shipment comment.'), $e);
121  }
122  return true;
123  }
124 
129  {
130  try {
131  $this->commentResource->save($entity);
132  } catch (\Exception $e) {
133  throw new CouldNotSaveException(__('Could not save the shipment comment.'), $e);
134  }
135 
136  try {
137  $shipment = $this->shipmentRepository->get($entity->getParentId());
138  $this->shipmentCommentSender->send($shipment, $entity->getIsCustomerNotified(), $entity->getComment());
139  } catch (\Exception $exception) {
140  $this->logger->critical($exception);
141  }
142 
143  return $entity;
144  }
145 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
getList(SearchCriteriaInterface $searchCriteria)
$searchCriteria
__construct(ShipmentCommentResourceInterface $commentResource, ShipmentCommentInterfaceFactory $commentFactory, ShipmentCommentSearchResultInterfaceFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor, ShipmentCommentSender $shipmentCommentSender=null, ShipmentRepositoryInterface $shipmentRepository=null, LoggerInterface $logger=null)
$entity
Definition: element.phtml:22
foreach($order->getItems() as $orderItem) $shipment