init
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\SubItemProduct\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class SubItem extends AbstractDb
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('subitem_product', 'subitem_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\SubItemProduct\Model\ResourceModel\SubItem;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected $_idFieldName = 'subitem_id';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(
|
||||
\Stemcell\SubItemProduct\Model\SubItem::class,
|
||||
\Stemcell\SubItemProduct\Model\ResourceModel\SubItem::class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\SubItemProduct\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Stemcell\SubItemProduct\Api\Data\SubItemInterface;
|
||||
|
||||
class SubItem extends AbstractModel implements SubItemInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init(\Stemcell\SubItemProduct\Model\ResourceModel\SubItem::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSubitemId()
|
||||
{
|
||||
return $this->getData(self::SUBITEM_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setSubitemId($subitemId)
|
||||
{
|
||||
return $this->setData(self::SUBITEM_ID, $subitemId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getProductId()
|
||||
{
|
||||
return $this->getData(self::PRODUCT_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setProductId($productId)
|
||||
{
|
||||
$this->setData(self::PRODUCT_ID, $productId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSubSku()
|
||||
{
|
||||
return $this->getData(self::SUB_SKU);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setSubSku(string $subSku)
|
||||
{
|
||||
return $this->setData(self::SUB_SKU, $subSku);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getBundleQty()
|
||||
{
|
||||
return (int)$this->getData(self::BUNDLE_QTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setBundleQty(int $bundleQty)
|
||||
{
|
||||
return $this->setData(self::BUNDLE_QTY, $bundleQty);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getStockQty()
|
||||
{
|
||||
return (int)$this->getData(self::STOCK_QTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setStockQty(int $stockQty)
|
||||
{
|
||||
return $this->setData(self::STOCK_QTY, $stockQty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\SubItemProduct\Model;
|
||||
|
||||
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
||||
use Magento\Framework\Exception\CouldNotDeleteException;
|
||||
use Magento\Framework\Exception\CouldNotSaveException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Stemcell\SubItemProduct\Api\Data\SubItemInterface;
|
||||
use Stemcell\SubItemProduct\Api\Data\SubItemInterfaceFactory;
|
||||
use Stemcell\SubItemProduct\Api\Data\SubItemSearchResultsInterfaceFactory;
|
||||
use Stemcell\SubItemProduct\Api\SubItemRepositoryInterface;
|
||||
use Stemcell\SubItemProduct\Model\ResourceModel\SubItem as ResourceSubItem;
|
||||
use Stemcell\SubItemProduct\Model\ResourceModel\SubItem\CollectionFactory as SubItemCollectionFactory;
|
||||
|
||||
class SubItemRepository implements SubItemRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var SubItemInterfaceFactory
|
||||
*/
|
||||
protected $subItemFactory;
|
||||
|
||||
/**
|
||||
* @var CollectionProcessorInterface
|
||||
*/
|
||||
protected $collectionProcessor;
|
||||
|
||||
/**
|
||||
* @var SubItemCollectionFactory
|
||||
*/
|
||||
protected $subItemCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var ResourceSubItem
|
||||
*/
|
||||
protected $resource;
|
||||
|
||||
/**
|
||||
* @var SubItem
|
||||
*/
|
||||
protected $searchResultsFactory;
|
||||
|
||||
|
||||
/**
|
||||
* @param ResourceSubItem $resource
|
||||
* @param SubItemInterfaceFactory $subItemFactory
|
||||
* @param SubItemCollectionFactory $subItemCollectionFactory
|
||||
* @param SubItemSearchResultsInterfaceFactory $searchResultsFactory
|
||||
* @param CollectionProcessorInterface $collectionProcessor
|
||||
*/
|
||||
public function __construct(
|
||||
ResourceSubItem $resource,
|
||||
SubItemInterfaceFactory $subItemFactory,
|
||||
SubItemCollectionFactory $subItemCollectionFactory,
|
||||
SubItemSearchResultsInterfaceFactory $searchResultsFactory,
|
||||
CollectionProcessorInterface $collectionProcessor
|
||||
) {
|
||||
$this->resource = $resource;
|
||||
$this->subItemFactory = $subItemFactory;
|
||||
$this->subItemCollectionFactory = $subItemCollectionFactory;
|
||||
$this->searchResultsFactory = $searchResultsFactory;
|
||||
$this->collectionProcessor = $collectionProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function save(SubItemInterface $subItem)
|
||||
{
|
||||
try {
|
||||
$this->resource->save($subItem);
|
||||
} catch (\Exception $exception) {
|
||||
throw new CouldNotSaveException(__(
|
||||
'Could not save the subItem: %1',
|
||||
$exception->getMessage()
|
||||
));
|
||||
}
|
||||
return $subItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get($subItemId)
|
||||
{
|
||||
$subItem = $this->subItemFactory->create();
|
||||
$this->resource->load($subItem, $subItemId);
|
||||
if (!$subItem->getId()) {
|
||||
throw new NoSuchEntityException(__('SubItem with id "%1" does not exist.', $subItemId));
|
||||
}
|
||||
return $subItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getList(
|
||||
\Magento\Framework\Api\SearchCriteriaInterface $criteria
|
||||
) {
|
||||
$collection = $this->subItemCollectionFactory->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(SubItemInterface $subItem)
|
||||
{
|
||||
try {
|
||||
$subItemModel = $this->subItemFactory->create();
|
||||
$this->resource->load($subItemModel, $subItem->getSubitemId());
|
||||
$this->resource->delete($subItemModel);
|
||||
} catch (\Exception $exception) {
|
||||
throw new CouldNotDeleteException(__(
|
||||
'Could not delete the SubItem: %1',
|
||||
$exception->getMessage()
|
||||
));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteById($subItemId)
|
||||
{
|
||||
return $this->delete($this->get($subItemId));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user