StaffRepository

StaffRepository
 avatar
user_3906099546
php
2 years ago
4.7 kB
0
Indexable
Never
<?php

namespace Isobar\Bai22\Model;

use Isobar\Bai22\Api\Data\StaffInterface;
use Isobar\Bai22\Model\ResourceModel\Staff\Collection;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Class StaffManagement
 * @package Isobar\Bai22\Model
 */
class StaffRepository implements \Isobar\Bai22\Api\StaffRepositoryInterface
{

    /**
     * @var \Isobar\Bai22\Model\StaffFactory
     */
    protected $staffFactory;

    /**
     * @var ResourceModel\Staff
     */
    protected $staffResource;

    /**
     * @var ResourceModel\Staff\CollectionFactory
     */
    protected $collectionFactory;
    private CollectionProcessorInterface $collectionProcessor;
    private \Isobar\Bai22\Api\Data\StaffSearchResultInterfaceFactory $searchResultFactory;

    /**
     * @var \Isobar\Bai22\Api\Data\StaffSearchResultInterfaceFactory
     */


    /**
     * StaffRepository constructor.
     * @param \Isobar\Bai22\Api\Data\StaffInterfaceFactory $staffFactory
     * @param ResourceModel\Staff $staffResource
     * @param ResourceModel\Staff\CollectionFactory $collectionFactory
     * @param CollectionProcessorInterface $collectionProcessor
     * @param \Isobar\Bai22\Api\Data\StaffSearchResultInterfaceFactory $searchResultFactory
     */
    public function __construct(
        \Isobar\Bai22\Api\Data\StaffInterfaceFactory                          $staffFactory,
        \Isobar\Bai22\Model\ResourceModel\Staff                   $staffResource,
        \Isobar\Bai22\Model\ResourceModel\Staff\CollectionFactory $collectionFactory,
        \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface $collectionProcessor,
        \Isobar\Bai22\Api\Data\StaffSearchResultInterfaceFactory $searchResultFactory
    )
    {
        $this->staffFactory = $staffFactory;
        $this->staffResource = $staffResource;
        $this->collectionFactory = $collectionFactory;
        $this->collectionProcessor = $collectionProcessor;
        $this->searchResultFactory = $searchResultFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function getById($id)
    {
        $staffModel = $this->staffFactory->create();
        $this->staffResource->load($staffModel, $id);
        if (!$staffModel->getId()) {
            throw new NoSuchEntityException(__('Unable to find staff data with ID ' . $id));
        }
        return $staffModel;
    }

    /**
     * {@inheritdoc}
     */
    public function save(StaffInterface $staffModel)
    {

        $this->staffResource->save($staffModel);
        return $staffModel;
    }

    /**
     * {@inheritdoc}
     */
    public function deleteById($id)
    {
        try {
            $staffModel = $this->staffFactory->create();
            $this->staffResource->load($staffModel, $id);
            $this->staffResource->delete($staffModel);
        } catch (\Exception $exception) {
            throw new CouldNotDeleteException(
                __('Could not delete the entry: {0}', $exception->getMessage())
            );
        }

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function getList()
    {
        $collection = $this->collectionFactory->create();
        $result = $collection->toArray();
        return json_encode($result);
    }


    /**
     * {@inheritdoc}
     */
    public function update(int $id_entity, StaffInterface $vimagento)
    {
        $staffModel = $this->staffFactory->create();
        $this->staffResource->load($staffModel, $id_entity);
        if (!$staffModel->getId()) {
            throw new NoSuchEntityException(__('Update Unable to find staff data with ID ' . $id_entity));
        }
        try {
            foreach ($vimagento as $key => $value) {

//            if ($staffModel[$key] !== $vimagento[$key]){
//                $staffModel->setData([$key, $value]);
//                $staffModel->save();
//            }

            }
        } catch (\Exception $e) {
            return $e;
        }

        return $staffModel;
    }

    /**
     * {@inheritdoc}
     */
    public function getEtc(SearchCriteriaInterface $searchCriteria)
    {
        $colection = $this->collectionFactory->create();
        $this->collectionProcessor->process($searchCriteria,$colection);
        $searchResult= $this->searchResultFactory->create();
        $searchResult->setSearchCriteria($searchCriteria);
        $searchResult->setItems($colection->getItems());
        $searchResult->setTotalCount($colection->getSize());
        return $searchResult;
    }
}