Files
2026-09-15 10:26:34 +08:00

149 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Stemcell\Webinar\Model;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Stemcell\Webinar\Api\Data\WebinarInterface;
use Stemcell\Webinar\Api\Data\WebinarInterfaceFactory;
use Stemcell\Webinar\Api\Data\WebinarSearchResultsInterfaceFactory;
use Stemcell\Webinar\Api\WebinarRepositoryInterface;
use Stemcell\Webinar\Model\ResourceModel\Webinar as ResourceWebinar;
use Stemcell\Webinar\Model\ResourceModel\Webinar\CollectionFactory as WebinarCollectionFactory;
class WebinarRepository implements WebinarRepositoryInterface
{
/**
* @var WebinarCollectionFactory
*/
protected $webinarCollectionFactory;
/**
* @var WebinarInterfaceFactory
*/
protected $webinarFactory;
/**
* @var CollectionProcessorInterface
*/
protected $collectionProcessor;
/**
* @var ResourceWebinar
*/
protected $resource;
/**
* @var Webinar
*/
protected $searchResultsFactory;
/**
* @param ResourceWebinar $resource
* @param WebinarInterfaceFactory $webinarFactory
* @param WebinarCollectionFactory $webinarCollectionFactory
* @param WebinarSearchResultsInterfaceFactory $searchResultsFactory
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
ResourceWebinar $resource,
WebinarInterfaceFactory $webinarFactory,
WebinarCollectionFactory $webinarCollectionFactory,
WebinarSearchResultsInterfaceFactory $searchResultsFactory,
CollectionProcessorInterface $collectionProcessor
) {
$this->resource = $resource;
$this->webinarFactory = $webinarFactory;
$this->webinarCollectionFactory = $webinarCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->collectionProcessor = $collectionProcessor;
}
/**
* @inheritDoc
*/
public function save(WebinarInterface $webinar)
{
try {
$this->resource->save($webinar);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__(
'Could not save the webinar: %1',
$exception->getMessage()
));
}
return $webinar;
}
/**
* @param $value
* @param $field
* @return WebinarInterface
* @throws NoSuchEntityException
*/
public function get($value, $field)
{
$webinar = $this->webinarFactory->create();
$this->resource->load($webinar, $value, $field);
if (!$webinar->getId()) {
throw new NoSuchEntityException(__('Webinar with %1 "%2" does not exist.', $field, $value));
}
return $webinar;
}
/**
* @inheritDoc
*/
public function getList(
\Magento\Framework\Api\SearchCriteriaInterface $criteria
) {
$collection = $this->webinarCollectionFactory->create();
$this->collectionProcessor->process($criteria, $collection);
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($criteria);
$items = [];
foreach ($collection as $model) {
$items[] = $model;
}
$searchResults->setItems($items);
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
/**
* @inheritDoc
*/
public function delete(WebinarInterface $webinar)
{
try {
$webinarModel = $this->webinarFactory->create();
$this->resource->load($webinarModel, $webinar->getWebinarId());
$this->resource->delete($webinarModel);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__(
'Could not delete the Webinar: %1',
$exception->getMessage()
));
}
return true;
}
/**
* @inheritDoc
*/
public function deleteById($webinarId)
{
return $this->delete($this->get($webinarId));
}
}