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\InvoiceCommentInterfaceFactory;
15 use Magento\Sales\Api\Data\InvoiceCommentSearchResultInterfaceFactory;
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 $invoiceCommentSender;
51 
55  private $invoiceRepository;
56 
60  private $logger;
61 
71  public function __construct(
72  InvoiceCommentResourceInterface $commentResource,
73  InvoiceCommentInterfaceFactory $commentFactory,
74  InvoiceCommentSearchResultInterfaceFactory $searchResultFactory,
75  CollectionProcessorInterface $collectionProcessor,
76  InvoiceCommentSender $invoiceCommentSender = null,
77  InvoiceRepositoryInterface $invoiceRepository = null,
78  LoggerInterface $logger = null
79  ) {
80  $this->commentResource = $commentResource;
81  $this->commentFactory = $commentFactory;
82  $this->searchResultFactory = $searchResultFactory;
83  $this->collectionProcessor = $collectionProcessor;
84  $this->invoiceCommentSender = $invoiceCommentSender
85  ?:ObjectManager::getInstance()->get(InvoiceCommentSender::class);
86  $this->invoiceRepository = $invoiceRepository
87  ?:ObjectManager::getInstance()->get(InvoiceRepositoryInterface::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(InvoiceCommentInterface $entity)
116  {
117  try {
118  $this->commentResource->delete($entity);
119  } catch (\Exception $e) {
120  throw new CouldNotDeleteException(__('Could not delete the invoice 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 invoice comment.'), $e);
134  }
135 
136  try {
137  $invoice = $this->invoiceRepository->get($entity->getParentId());
138  $this->invoiceCommentSender->send($invoice, $entity->getIsCustomerNotified(), $entity->getComment());
139  } catch (\Exception $exception) {
140  $this->logger->critical($exception);
141  }
142 
143  return $entity;
144  }
145 }
getList(SearchCriteriaInterface $searchCriteria)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$searchCriteria
__construct(InvoiceCommentResourceInterface $commentResource, InvoiceCommentInterfaceFactory $commentFactory, InvoiceCommentSearchResultInterfaceFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor, InvoiceCommentSender $invoiceCommentSender=null, InvoiceRepositoryInterface $invoiceRepository=null, LoggerInterface $logger=null)
$invoice
$entity
Definition: element.phtml:22