namespace Magento\Framework\ObjectManager\Code\Generator;

/**
 * Repository class for @see \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
 */
class SampleRepository implements \Magento\Framework\ObjectManager\Code\Generator\SampleRepositoryInterface
{
    /**
     * sampleInterfacePersistor
     *
     * @var \Magento\Framework\ObjectManager\Code\Generator\SampleInterfacePersistor
     */
    protected $sampleInterfacePersistor = null;

    /**
     * Collection Factory
     *
     * @var \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory
     */
    protected $sampleInterfaceSearchResultFactory = null;

    /**
     * \Magento\Framework\ObjectManager\Code\Generator\SampleInterface[]
     *
     * @var array
     */
    protected $registry = array(
        
    );

    /**
     * Extension attributes join processor.
     *
     * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
     */
    protected $extensionAttributesJoinProcessor = null;

    /**
     * Repository constructor
     *
     * @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface $sampleInterfacePersistor
     * @param \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory $sampleInterfaceSearchResultFactory
     * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
     */
    public function __construct(\Magento\Framework\ObjectManager\Code\Generator\SampleInterfacePersistor $sampleInterfacePersistor, \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory $sampleInterfaceSearchResultFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor)
    {
        $this->sampleInterfacePersistor = $sampleInterfacePersistor;
        $this->sampleInterfaceSearchResultFactory = $sampleInterfaceSearchResultFactory;
        $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
    }

    /**
     * load entity
     *
     * @param int $id
     * @return \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function get($id)
    {
        if (!$id) {
            throw new \Magento\Framework\Exception\InputException('ID required');
        }
        if (!isset($this->registry[$id])) {
            $entity = $this->sampleInterfacePersistor->loadEntity($id);
            if (!$entity->getId()) {
                throw new \Magento\Framework\Exception\NoSuchEntityException('Requested entity doesn\'t exist');
            }
            $this->registry[$id] = $entity;
        }
        return $this->registry[$id];
    }

    /**
     * Register entity to create
     *
     * @param array $data
     * @return \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
     */
    public function create(\Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity)
    {
        return $this->sampleInterfacePersistor->registerNew($entity);
    }

    /**
     * Register entity to create
     *
     * @param array $data
     * @return \Magento\Framework\ObjectManager\Code\Generator\SampleRepository
     */
    public function createFromArray(array $data)
    {
        return $this->sampleInterfacePersistor->registerFromArray($data);
    }

    /**
     * Find entities by criteria
     *
     * @param \Magento\Framework\Api\SearchCriteria $searchCriteria
     * @return \Magento\Framework\ObjectManager\Code\Generator\SampleInterface[]
     */
    public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria)
    {
        $collection = $this->sampleInterfaceSearchResultFactory->create();
        $this->extensionAttributesJoinProcessor->process($collection);
        foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
            foreach ($filterGroup->getFilters() as $filter) {
                $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
                $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
            }
        }
        $collection->setCurPage($searchCriteria->getCurrentPage());
        $collection->setPageSize($searchCriteria->getPageSize());
        return $collection;
    }

    /**
     * Register entity to delete
     *
     * @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity
     */
    public function remove(\Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity)
    {
        $this->sampleInterfacePersistor->registerDeleted($entity);
    }

    /**
     * Register entity to delete
     *
     * @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity
     * @return bool
     */
    public function delete(\Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity)
    {
        $this->sampleInterfacePersistor->registerDeleted($entity);
        return $this->sampleInterfacePersistor->doPersistEntity($entity);
    }

    /**
     * Delete entity by Id
     *
     * @param int $id
     * @return bool
     */
    public function deleteById($id)
    {
        $entity = $this->get($id);
        $this->sampleInterfacePersistor->registerDeleted($entity);
        return $this->sampleInterfacePersistor->doPersistEntity($entity);
    }

    /**
     * Perform persist operations
     */
    public function flush()
    {
        $ids = $this->sampleInterfacePersistor->doPersist();
        foreach ($ids as $id) {
        unset($this->registry[$id]);
        }
    }

    /**
     * Perform persist operations for one entity
     *
     * @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity
     * @return \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
     */
    public function save(\Magento\Framework\ObjectManager\Code\Generator\SampleInterface $entity)
    {
        $this->sampleInterfacePersistor->doPersistEntity($entity);
        return $entity;
    }
}
