Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 6151418e authored by Nicolas's avatar Nicolas
Browse files

Création des model et resourceModel

parent 058ffcee
No related branches found
No related tags found
No related merge requests found
<?php
namespace NicolasBejean\Sales\Model;
use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\Sales\Api\GetOrderExporterByOrderIdInterface;
use \NicolasBejean\Sales\Api\Data\OrderExporterInterface;
use \Magento\Framework\Exception\NoSuchEntityException;
use \NicolasBejean\Sales\Model\ResourceModel\OrderExporter as OrderExporterResource;
/**
* Class GetOrderExporterByIdentifier
*
* @category PHP
* @package NicolasBejean\Sales\Model
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class GetOrderExporterByOrderId implements GetOrderExporterByOrderIdInterface
{
/**
* @var OrderExporterFactory
*/
private $orderExporterFactory;
/**
* @var OrderExporterResource
*/
private $orderExporterResource;
/**
* @param OrderExporterFactory $orderExporterFactory
* @param OrderExporterResource $orderExporterResource
*/
public function __construct(
OrderExporterFactory $orderExporterFactory,
OrderExporterResource $orderExporterResource
) {
$this->orderExporterFactory = $orderExporterFactory;
$this->orderExporterResource = $orderExporterResource;
}
/**
* @inheritdoc
* @throws LocalizedException
*/
public function execute(string $orderId) : OrderExporterInterface
{
$orderExporter = $this->orderExporterFactory->create();
$this->orderExporterResource->load($orderExporter, $orderId, OrderExporterInterface::ORDER_ID);
if (!$orderExporter->getId()) {
throw new NoSuchEntityException(__('The order exporter with the \'%1\' order id doesn\'t exist.', $orderId));
}
return $orderExporter;
}
}
<?php
namespace NicolasBejean\Sales\Model;
use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\Sales\Api\Data\OrderExporterInterface;
use \Magento\Framework\DataObject\IdentityInterface;
use \Magento\Framework\Model\AbstractModel;
/**
* Class OrderExporter
*
* @category PHP
* @package NicolasBejean\Sales\Model
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class OrderExporter extends AbstractModel implements OrderExporterInterface, IdentityInterface
{
/**
* Cache tag
*
* @var string
*/
const CACHE_TAG = 'orderexporter';
/**
* Prefix of model events names
*
* @var string
*/
protected $eventPrefix = 'orderexporter';
/**
* @return void
*/
protected function _construct()
{
$this->_init(ResourceModel\OrderExporter::class);
}
/**
* Prevent recursion
*
* @return AbstractModel
* @throws LocalizedException
*/
public function beforeSave()
{
if ($this->hasDataChanges()) {
$this->setUpdateTime(null);
}
$needle = 'entity_id="' . $this->getId() . '"';
if (false == strstr($this->getOrderId(), $needle)) {
return parent::beforeSave();
}
throw new LocalizedException(
__('Make sure that item does not reference the item itself.')
);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getOrderId()];
}
/**
* Retrieve Entity ID
*
* @return int
*/
public function getId()
{
return $this->getData(self::ENTITY_ID);
}
/**
* Retrieve Order ID
*
* @return string
*/
public function getOrderId()
{
return $this->getData(self::ORDER_ID);
}
/**
* Retrieve Is Exported
*
* @return string
*/
public function getIsExported()
{
return (string)$this->getData(self::IS_EXPORTED);
}
/**
* Retrieve Creation Time
*
* @return string
*/
public function getCreationTime()
{
return $this->getData(self::CREATION_TIME);
}
/**
* Retrieve Update Time
*
* @return string
*/
public function getUpdateTime()
{
return $this->getData(self::UPDATE_TIME);
}
/**
* Set Entity ID
*
* @param int $id
* @return OrderExporterInterface
*/
public function setId($id)
{
return $this->setData(self::ENTITY_ID, $id);
}
/**
* Set Order ID
*
* @param string $orderId
* @return OrderExporterInterface
*/
public function setOrderId($orderId)
{
return $this->setData(self::ORDER_ID, $orderId);
}
/**
* Set Is Exported
*
* @param string $isExported
* @return OrderExporterInterface
*/
public function setIsExported($isExported)
{
return $this->setData(self::IS_EXPORTED, $isExported);
}
/**
* Set Creation Time
*
* @param string $creationTime
* @return OrderExporterInterface
*/
public function setCreationTime($creationTime)
{
return $this->setData(self::CREATION_TIME, $creationTime);
}
/**
* Set Update Time
*
* @param string $updateTime
* @return OrderExporterInterface
*/
public function setUpdateTime($updateTime)
{
return $this->setData(self::UPDATE_TIME, $updateTime);
}
}
<?php
namespace NicolasBejean\Sales\Model;
use \Magento\Framework\Api\SearchCriteriaInterface;
use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\Sales\Api\Data\OrderExporterInterface;
use \NicolasBejean\Sales\Api\Data\OrderExporterInterfaceFactory;
use \NicolasBejean\Sales\Api\Data\OrderExporterSearchResultsInterface;
use \NicolasBejean\Sales\Api\OrderExporterRepositoryInterface;
use \NicolasBejean\Sales\Api\Data;
use \NicolasBejean\Sales\Model\ResourceModel\OrderExporter as Resource;
use \NicolasBejean\Sales\Model\ResourceModel\OrderExporter\Collection;
use \NicolasBejean\Sales\Model\ResourceModel\OrderExporter\CollectionFactory as OrderExporterCollectionFactory;
use \Magento\Framework\Api\DataObjectHelper;
use \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Reflection\DataObjectProcessor;
use \Exception;
/**
* Class OrderExporterRepository
*
* @category PHP
* @package NicolasBejean\Sales\Model
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class OrderExporterRepository implements OrderExporterRepositoryInterface
{
/**
* @var Resource
*/
protected $resource;
/**
* @var OrderExporterFactory
*/
protected $orderExporterFactory;
/**
* @var OrderExporterCollectionFactory
*/
protected $orderExporterCollectionFactory;
/**
* @var Data\OrderExporterSearchResultsInterfaceFactory
*/
protected $searchResultsFactory;
/**
* @var DataObjectHelper
*/
protected $dataObjectHelper;
/**
* @var DataObjectProcessor
*/
protected $dataObjectProcessor;
/**
* @var OrderExporterInterfaceFactory
*/
protected $dataOrderExporterFactory;
/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;
/**
* @param Resource $resource
* @param OrderExporterFactory $orderExporterFactory
* @param OrderExporterInterfaceFactory $dataOrderExporterFactory
* @param OrderExporterCollectionFactory $orderExporterCollectionFactory
* @param Data\OrderExporterSearchResultsInterfaceFactory $searchResultsFactory
* @param DataObjectHelper $dataObjectHelper
* @param DataObjectProcessor $dataObjectProcessor
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
Resource $resource,
OrderExporterFactory $orderExporterFactory,
OrderExporterInterfaceFactory $dataOrderExporterFactory,
OrderExporterCollectionFactory $orderExporterCollectionFactory,
Data\OrderExporterSearchResultsInterfaceFactory $searchResultsFactory,
DataObjectHelper $dataObjectHelper,
DataObjectProcessor $dataObjectProcessor,
CollectionProcessorInterface $collectionProcessor = null
) {
$this->resource = $resource;
$this->orderExporterFactory = $orderExporterFactory;
$this->orderExporterCollectionFactory = $orderExporterCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->dataOrderExporterFactory = $dataOrderExporterFactory;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->collectionProcessor = $collectionProcessor;
}
/**
* Save Order Exporter data
*
* @param OrderExporterInterface $orderExporter
* @return OrderExporterInterface
* @throws CouldNotSaveException
*/
public function save(OrderExporterInterface $orderExporter)
{
try {
$this->resource->save($orderExporter);
} catch (Exception $exception) {
throw new CouldNotSaveException(__($exception->getMessage()));
}
return $orderExporter;
}
/**
* Load Order Exporter data by given Order Exporter Identity
*
* @param string $id
* @return OrderExporter
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getById($id)
{
/** @var OrderExporter $orderExporter */
$orderExporter = $this->orderExporterFactory->create();
$this->resource->load($orderExporter, $id);
if (!$orderExporter->getId()) {
throw new NoSuchEntityException(__('The order exporter with the \'%1\' ID doesn\'t exist.', $id));
}
return $orderExporter;
}
/**
* Load Order Exporter data collection by given search criteria
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @param SearchCriteriaInterface $criteria
* @return OrderExporterSearchResultsInterface
*/
public function getList(SearchCriteriaInterface $criteria)
{
/** @var Collection $collection */
$collection = $this->orderExporterCollectionFactory->create();
$this->collectionProcessor->process($criteria, $collection);
/** @var OrderExporterSearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($criteria);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
/**
* Delete Order Exporter
*
* @param OrderExporterInterface $orderExporter
* @return bool
* @throws CouldNotDeleteException
*/
public function delete(OrderExporterInterface $orderExporter)
{
try {
$this->resource->delete($orderExporter);
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
return true;
}
/**
* Delete Order Exporter by given Order Exporter Identity
*
* @param string $id
* @return bool
* @throws CouldNotDeleteException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function deleteById($id)
{
return $this->delete($this->getById($id));
}
}
<?php
namespace NicolasBejean\Sales\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class OrderExporter
*
* @category PHP
* @package NicolasBejean\Sales\Model\ResourceModel
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class OrderExporter extends AbstractDb
{
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('nicolasbejean_sales_orderexporter', 'entity_id');
}
}
<?php
namespace NicolasBejean\Sales\Model\ResourceModel\OrderExporter;
use \NicolasBejean\Sales\Model\OrderExporter as OrderExporterModel;
use \NicolasBejean\Sales\Model\ResourceModel\OrderExporter as OrderExporterResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
/**
* Class Collection
*
* @category PHP
* @package NicolasBejean\Sales\Model\ResourceModel\OrderExporter
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Collection extends AbstractCollection
{
/**
* @var string
*/
protected $_idFieldName = 'entity_id';
/**
* Event prefix
*
* @var string
*/
protected $eventPrefix = 'orderexporter_collection';
/**
* Event object
*
* @var string
*/
protected $eventObject = 'orderexporter_collection';
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(OrderExporterModel::class, OrderExporterResourceModel::class);
$this->_map['fields']['entity_id'] = 'main_table.entity_id';
}
/**
* Returns pairs order_id - is_exported
*
* @return array
*/
public function toOptionArray()
{
return $this->_toOptionArray('order_id', 'is_exported');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment