[ create a new paste ] login | about

Link: http://codepad.org/KNt5QAGZ    [ raw code | fork | 1 comment ]

PHP, pasted on Jun 19:
<?php
/**
 * THIS IS THE REPOSITORY
 */

namespace Namespace\Custom\Model;

use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\NoSuchEntityException;
use Namespace\Vendor\Api\Data\CustomSearchResultsInterface;
use Namespace\Custom\Api\Data;
use Namespace\Custom\Api\CustomRepositoryInterface;
use Namespace\Custom\Model\ResourceModel\Custom as CustomResource;
use Namespace\Custom\Model\ResourceModel\Custom\CollectionFactory;

class CustomRepository implements CustomRepositoryInterface
{

    /**
     * @var customResource
     */
    private $customResource;
    /**
     * @var customFactory
     */
    private $customFactory;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;
    /**
     * @var CustomSearchResultsInterfaceFactory
     */
    private $searchResultsFactory;

    public function __construct(
        CustomResource $customResource,
        CustomFactory $customFactory,
        CollectionFactory $collectionFactory,
        CustomSearchResultsInterfaceFactory $searchResultsFactory
    )
    {
        $this->customResource = $customResource;
        $this->customFactory = $customFactory;
        $this->collectionFactory = $collectionFactory;
        $this->searchResults = $searchResultsFactory;
    }

    /**
     * @param \Namespace\Custom\Api\Data\CustomInterface $custom
     * @return int
     */
    public function save(\Namespace\Custom\Api\Data\CustomInterface $custom)
    {
        $this->customResource->save($custom);
        return $custom->getId();
    }

    /**
     * @param $customId
     * @return \Namespace\Custom\Api\Data\CustomInterface int
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getById($customId)
    {
        $Custom = $this->customFactory->create();
        $this->customResource->load($custom, $customId);
        if(!$Custom->getId()) {
            throw new NoSuchEntityException('Custom does not exist');
        }
        return $custom;
    }

    /**
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return \Namespace\Custom\Api\Data\CustomSearchResultsInterface
     */
    public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
        $collection = $this->collectionFactory->create();
        foreach ($searchCriteria->getFilterGroups() as $group) {
            $this->addFilterGroupToCollection($group, $collection);
        }
        /** @var Magento\Framework\Api\SortOrder $sortOrder */
        foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
            $field = $sortOrder->getField();
            $collection->addOrder(
                $field,
                $this->getDirection($sortOrder->getDirection())
            );

        }

        $collection->setCurPage($searchCriteria->getCurrentPage());
        $collection->setPageSize($searchCriteria->getPageSize());
        $collection->load();
        $searchResults = $this->searchResultsFactory->create();
        $searchResults->setCriteria($searchCriteria);

        $customs=[];
        foreach ($collection as $Custom){
            $Customs[] = $Custom;
        }
        $searchResults->setItems($customs);
        $searchResults->setTotalCount($collection->getSize());
        return $searchResults;
    }

    /**
     * @param int $customId
     * @return bool
     */
    public function delete($customId)
    {
        $custom = $this->customFactory->create();
        $custom->setId($customId);
        if( $this->customResource->delete($custom)){
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param int $customId
     * @return string
     */
    public function getAssociatedProductsIds($customId)
    {
        $productIds = $this->customResource
                        ->getAssociatedProductIds($customId);
        return json_encode($productIds);
    }

    private function getDirection($direction)
    {
        return $direction == SortOrder::SORT_ASC ?: SortOrder::SORT_DESC;
    }

    /**
     * @param \Magento\Framework\Api\Search\FilterGroup $group
     * @param CustomResource\Collection $collection
     */
    private function addFilterGroupToCollection($group, $collection)
    {
        $fields = [];
        $conditions = [];

        foreach($group->getFilters() as $filter){
            $condition = $filter->getConditionType() ?: 'eq';
            $field = $filter->getField();
            $value = $filter->getValue();
            $fields[] = $field;
            $conditions[] = [$condition=>$value];

        }
        $collection->addFieldToFilter($fields, $conditions);
    }
}


Create a new paste based on this one


Comments:
posted by hollowghost on Dec 13
private $searchResultsFactory;
is never assigned, and
$this->searchResults
is assigned but never used.
reply