saxulum/saxulum-model-importer

This package is abandoned and no longer maintained. No replacement package was suggested.

Saxulum Model Importer

2.1.0 2016-07-17 10:56 UTC

This package is not auto-updated.

Last update: 2020-09-22 19:09:15 UTC


README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

Description

A simple to use model importer, as a user you do not need to check if a create or update is needed, to loop...

Requirements

  • php: ~5.5|~7.0
  • psr/log: ~1.0

Installation

Through Composer as saxulum/saxulum-model-importer.

Usage

Sample Implementation using Doctrine 2 ORM

$em = ...

$importer = new Importer(new Reader($em), new Writer($em));
$importer->import();
class Reader implements ReaderInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @ReaderModelInterface[]|array
     */
    public function getReaderModels($offset, $limit)
    {
        $qb = $this->em->getRepository(ReaderEntity::class)->createQueryBuilder('r');
        $qb->setFirstResult($offset);
        $qb->setMaxResults($limit);

        return $qb->getQuery()->getResult();
    }

    public function clearReaderModels()
    {
        $this->em->clear(ReaderEntity::class);
    }
}
class ReaderEntity implements ReaderModelInterface
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var string
     */
    protected $name;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return int
     */
    public function getImportIdentifier()
    {
        return $this->getId();
    }
}
class Writer implements WriterInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @param ReaderModelInterface $readerModel
     *
     * @return WriterModelInterface|null
     */
    public function findWriterModel(ReaderModelInterface $readerModel)
    {
        return $this->em->getRepository(WriterEntity::class)
            ->findOneBy(['importIdentifier' => $readerModel->getImportIdentifier()]);
    }

    /**
     * @param ReaderModelInterface $readerModel
     *
     * @return WriterModelInterface
     *
     * @throws NotImportableException
     */
    public function createWriterModel(ReaderModelInterface $readerModel)
    {
        $writerModel = new WriterEntity();
        $writerModel->setName($readerModel->getName());

        return $writerModel;
    }

    /**
     * @param WriterModelInterface $writerModel
     * @param ReaderModelInterface $readerModel
     *
     * @throws NotImportableException
     */
    public function updateWriterModel(WriterModelInterface $writerModel, ReaderModelInterface $readerModel)
    {
        $writerModel->setName($readerModel->getName());
    }

    /**
     * @param WriterModelInterface $writerModel
     *
     * @throws NotImportableException
     */
    public function persistWriterModel(WriterModelInterface $writerModel)
    {
        $this->em->persist($writerModel);
    }

    public function flushWriterModels(array $writeModels)
    {
        $this->em->flush($writeModels);
    }

    public function clearWriterModels()
    {
        $this->em->clear(WriterEntity::class);
    }

    /**
     * @param \DateTime $lastImportDate
     */
    public function removeWriterModels(\DateTime $lastImportDate)
    {
        $qb = $this->em->createQueryBuilder();
        $qb->delete(WriterEntity::class, 'w');
        $qb->where(
            $qb->expr()->orX(
                $qb->expr()->isNull('w.lastImportDate'),
                $qb->expr()->neq('w.lastImportDate', ':lastImportDate')
            )
        );
        $qb->setParameter('lastImportDate', $lastImportDate);

        $qb->getQuery()->execute();
    }
}
class WriterEntity implements WriterModelInterface
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var string
     */
    protected $name;

    /**
     * @var int
     */
    protected $importIdentifier;

    /**
     * @var \DateTime
     */
    protected $lastImportDate;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param int $importIdentifier
     */
    public function setImportIdentifier($importIdentifier)
    {
        $this->importIdentifier = $importIdentifier;
    }

    /**
     * @return int
     */
    public function getImportIdentifier()
    {
        return $this->importIdentifier;
    }

    /**
     * @param \DateTime $lastImportDate
     */
    public function setLastImportDate(\DateTime $lastImportDate)
    {
        $this->lastImportDate = $lastImportDate;
    }

    /**
     * @return \DateTime
     */
    public function getLastImportDate()
    {
        return $this->lastImportDate;
    }
}

Copyright

Dominik Zogg dominik.zogg@gmail.com