Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckoutAgreementsRepository.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory as AgreementCollectionFactory;
19 
26 {
32  private $collectionFactory;
33 
39  private $storeManager;
40 
46  private $scopeConfig;
47 
51  private $resourceModel;
52 
56  private $agreementFactory;
57 
61  private $extensionAttributesJoinProcessor;
62 
66  private $agreementsList;
67 
71  private $activeStoreAgreementsFilter;
72 
86  public function __construct(
87  AgreementCollectionFactory $collectionFactory,
88  StoreManagerInterface $storeManager,
89  ScopeConfigInterface $scopeConfig,
90  AgreementResource $agreementResource,
91  AgreementFactory $agreementFactory,
92  JoinProcessorInterface $extensionAttributesJoinProcessor,
93  \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $agreementsList = null,
94  ActiveStoreAgreementsFilter $activeStoreAgreementsFilter = null
95  ) {
96  $this->collectionFactory = $collectionFactory;
97  $this->storeManager = $storeManager;
98  $this->scopeConfig = $scopeConfig;
99  $this->resourceModel = $agreementResource;
100  $this->agreementFactory = $agreementFactory;
101  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
102  $this->agreementsList = $agreementsList ?: ObjectManager::getInstance()->get(
103  \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
104  );
105  $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter ?: ObjectManager::getInstance()->get(
106  ActiveStoreAgreementsFilter::class
107  );
108  }
109 
115  public function getList()
116  {
117  if (!$this->scopeConfig->isSetFlag('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE)) {
118  return [];
119  }
120  return $this->agreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria());
121  }
122 
126  public function save(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data, $storeId = null)
127  {
128  $id = $data->getAgreementId();
129 
130  if ($id) {
131  $data = $this->get($id, $storeId)->addData($data->getData());
132  }
133  if ($storeId === null) {
134  $storeId = $this->storeManager->getStore()->getId();
135  }
136  $data->setStores([$storeId]);
137  try {
138  $this->resourceModel->save($data);
139  } catch (\Exception $e) {
140  throw new \Magento\Framework\Exception\CouldNotSaveException(
141  __('The "%1" checkout agreement couldn\'t be saved.', $data->getAgreementId())
142  );
143  }
144  return $data;
145  }
146 
151  {
152  try {
153  $this->resourceModel->delete($data);
154  } catch (\Exception $e) {
155  throw new \Magento\Framework\Exception\CouldNotDeleteException(
156  __('The "%1" checkout agreement couldn\'t be removed.', $data->getAgreementId())
157  );
158  }
159  return true;
160  }
161 
165  public function deleteById($id)
166  {
167  $model = $this->get($id);
168  $this->delete($model);
169  return true;
170  }
171 
175  public function get($id, $storeId = null)
176  {
178  $agreement = $this->agreementFactory->create();
179  $this->resourceModel->load($agreement, $id);
180  if (!$agreement->getId()) {
181  throw new NoSuchEntityException(
182  __('A checkout agreement with the "%1" specified ID wasn\'t found. Verify the ID and try again.', $id)
183  );
184  }
185  return $agreement;
186  }
187 }
$id
Definition: fieldset.phtml:14
__construct(AgreementCollectionFactory $collectionFactory, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, AgreementResource $agreementResource, AgreementFactory $agreementFactory, JoinProcessorInterface $extensionAttributesJoinProcessor, \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $agreementsList=null, ActiveStoreAgreementsFilter $activeStoreAgreementsFilter=null)
$storeManager
__()
Definition: __.php:13
save(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data, $storeId=null)