diff --git a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php index dc28358..8d07bab 100644 --- a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php +++ b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php @@ -6,7 +6,6 @@ namespace DigiComp\Sequence\Command; * * * */ -use Doctrine\DBAL\DBALException; use TYPO3\Flow\Annotations as Flow; /** @@ -14,21 +13,23 @@ use TYPO3\Flow\Annotations as Flow; * * @Flow\Scope("singleton") */ -class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController { +class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController +{ - /** - * @var \DigiComp\Sequence\Service\SequenceGenerator - * @Flow\Inject - */ - protected $sequenceGenerator; + /** + * @var \DigiComp\Sequence\Service\SequenceGenerator + * @Flow\Inject + */ + protected $sequenceGenerator; - /** - * Sets minimum number for sequence generator - * - * @param int $to - * @param string $type - */ - public function advanceCommand($to, $type) { - $this->sequenceGenerator->advanceTo($to, $type); - } + /** + * Sets minimum number for sequence generator + * + * @param int $to + * @param string $type + */ + public function advanceCommand($to, $type) + { + $this->sequenceGenerator->advanceTo($to, $type); + } } diff --git a/Classes/DigiComp/Sequence/Domain/Model/Insert.php b/Classes/DigiComp/Sequence/Domain/Model/Insert.php index 7cad0b1..3665280 100644 --- a/Classes/DigiComp/Sequence/Domain/Model/Insert.php +++ b/Classes/DigiComp/Sequence/Domain/Model/Insert.php @@ -8,6 +8,7 @@ namespace DigiComp\Sequence\Domain\Model; use TYPO3\Flow\Annotations as Flow; use Doctrine\ORM\Mapping as ORM; + /** * SequenceInsert * @@ -15,59 +16,65 @@ use Doctrine\ORM\Mapping as ORM; * @Flow\Scope("prototype") * @Flow\Entity */ -class Insert { +class Insert +{ - /** - * @var int - * @ORM\Id - * @Flow\Identity - */ - protected $number; + /** + * @var int + * @ORM\Id + * @Flow\Identity + */ + protected $number; - /** - * @var string - * @ORM\Id - * @Flow\Identity - */ - protected $type; + /** + * @var string + * @ORM\Id + * @Flow\Identity + */ + protected $type; - /** - * @param int $number - * @param string $type - */ - public function __construct($number, $type) { - $this->setType($type); - $this->setNumber($number); - } + /** + * @param int $number + * @param string|object $type + */ + public function __construct($number, $type) + { + $this->setType($type); + $this->setNumber($number); + } - /** - * @param int $number - */ - public function setNumber($number) { - $this->number = $number; - } + /** + * @param int $number + */ + public function setNumber($number) + { + $this->number = $number; + } - /** - * @return int - */ - public function getNumber() { - return $this->number; - } + /** + * @return int + */ + public function getNumber() + { + return $this->number; + } - /** - * @param string|object $type - */ - public function setType($type) { - if (is_object($type)) { - $type = get_class($type); - } - $this->type = $type; - } + /** + * @param string|object $type + */ + public function setType($type) + { + if (is_object($type)) { + $type = get_class($type); + } + $this->type = $type; + } - /** - * @return string - */ - public function getType() { - return $this->type; - } -} \ No newline at end of file + /** + * @return string + */ + public function getType() + { + return $this->type; + } +} diff --git a/Classes/DigiComp/Sequence/Service/Exception.php b/Classes/DigiComp/Sequence/Service/Exception.php index ae9d6c3..6b21953 100644 --- a/Classes/DigiComp/Sequence/Service/Exception.php +++ b/Classes/DigiComp/Sequence/Service/Exception.php @@ -7,9 +7,11 @@ namespace DigiComp\Sequence\Service; * */ use TYPO3\Flow\Annotations as Flow; + /** * SequenceException */ -class Exception extends \Exception { +class Exception extends \Exception +{ } diff --git a/Classes/DigiComp/Sequence/Service/SequenceGenerator.php b/Classes/DigiComp/Sequence/Service/SequenceGenerator.php index 8b69bac..cc072d9 100644 --- a/Classes/DigiComp/Sequence/Service/SequenceGenerator.php +++ b/Classes/DigiComp/Sequence/Service/SequenceGenerator.php @@ -10,91 +10,104 @@ use Doctrine\DBAL\DBALException; use TYPO3\Flow\Annotations as Flow; /** - * A SequenceNumber generator (should be DB-agnostic) + * A SequenceNumber generator working for transactional databases * * - * Thoughts: We could make the step-range configurable, and if > 1 we could return new keys immediately for this request, - * as we "reserved" the space between. + * Thoughts: We could make the step-range configurable, and if > 1 we could return new keys immediately for this + * request, as we "reserved" the space between. * * @Flow\Scope("singleton") */ -class SequenceGenerator { +class SequenceGenerator +{ - /** - * @var \Doctrine\Common\Persistence\ObjectManager - * @Flow\Inject - */ - protected $_em; + /** + * @var \Doctrine\Common\Persistence\ObjectManager + * @Flow\Inject + */ + protected $entityManager; - /** - * @var \TYPO3\Flow\Reflection\ReflectionService - * @Flow\Inject - */ - protected $reflectionService; + /** + * @var \TYPO3\Flow\Reflection\ReflectionService + * @Flow\Inject + */ + protected $reflectionService; - /** - * @var \TYPO3\Flow\Log\SystemLoggerInterface - * @Flow\Inject - */ - protected $systemLogger; + /** + * @var \TYPO3\Flow\Log\SystemLoggerInterface + * @Flow\Inject + */ + protected $systemLogger; - /** - * @param string|object $type - * @throws \DigiComp\Sequence\Service\Exception - * @return int - */ - public function getNextNumberFor($type) { - if (is_object($type)) { - $type = $this->reflectionService->getClassNameByObject($type); - } - if (!$type) { - throw new Exception('No Type given'); - } - $count = $this->getLastNumberFor($type); + /** + * @param string|object $type + * + * @throws \DigiComp\Sequence\Service\Exception + * @return int + */ + public function getNextNumberFor($type) + { + if (is_object($type)) { + $type = $this->reflectionService->getClassNameByObject($type); + } + if (!$type) { + throw new Exception('No Type given'); + } + $count = $this->getLastNumberFor($type); - //TODO: Check for maximal tries, or similar - do { - $count = $count+1; - } while (! $this->validateFreeNumber($count, $type)); - return $count; - } + //TODO: Check for maximal tries, or similar + //TODO: Let increment be configurable per type + do { + $count = $count + 1; + } while (!$this->validateFreeNumber($count, $type)); + return $count; + } - protected function validateFreeNumber($count, $type) { - $em = $this->_em; - /** @var $em \Doctrine\ORM\EntityManager */ - try { - $em->getConnection()->insert('digicomp_sequence_domain_model_insert', array('number' => $count, 'type' => $type)); - return true; - } catch (\PDOException $e) { - return false; - } catch (DBALException $e) { - if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) { - // Do nothing, new Doctrine handling hides the above error - } else { - $this->systemLogger->logException($e); - } - } catch (\Exception $e) { - $this->systemLogger->logException($e); - } - return false; - } + protected function validateFreeNumber($count, $type) + { + $em = $this->entityManager; + /** @var $em \Doctrine\ORM\EntityManager */ + try { + $em->getConnection()->insert( + 'digicomp_sequence_domain_model_insert', + ['number' => $count, 'type' => $type] + ); + return true; + } catch (\PDOException $e) { + return false; + } catch (DBALException $e) { + if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) { + // Do nothing, new Doctrine handling hides the above error + } else { + $this->systemLogger->logException($e); + } + } catch (\Exception $e) { + $this->systemLogger->logException($e); + } + return false; + } - public function advanceTo($to, $type) { - return ($this->validateFreeNumber($to, $type)); - } + public function advanceTo($to, $type) + { + return ($this->validateFreeNumber($to, $type)); + } - /** - * @param $type - * @return int - */ - public function getLastNumberFor($type) { - /** @var $em \Doctrine\ORM\EntityManager */ - $em = $this->_em; + /** + * @param $type + * + * @return int + */ + public function getLastNumberFor($type) + { + /** @var $em \Doctrine\ORM\EntityManager */ + $em = $this->entityManager; - $result = $em->getConnection()->executeQuery('SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', array('type' => $type)); - $count = $result->fetchAll(); - $count = $count[0]['count']; - return $count; - } - -} \ No newline at end of file + $result = $em->getConnection()->executeQuery( + 'SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', + array('type' => $type) + ); + $count = $result->fetchAll(); + $count = $count[0]['count']; + return $count; + } +} diff --git a/Migrations/Mysql/Version20140505093853.php b/Migrations/Mysql/Version20140505093853.php index 76c4991..ad24715 100644 --- a/Migrations/Mysql/Version20140505093853.php +++ b/Migrations/Mysql/Version20140505093853.php @@ -2,32 +2,39 @@ namespace TYPO3\Flow\Persistence\Doctrine\Migrations; use Doctrine\DBAL\Migrations\AbstractMigration, - Doctrine\DBAL\Schema\Schema; + Doctrine\DBAL\Schema\Schema; /** * Auto-generated Migration: Please modify to your need! */ -class Version20140505093853 extends AbstractMigration { +class Version20140505093853 extends AbstractMigration +{ - /** - * @param Schema $schema - * @return void - */ - public function up(Schema $schema) { - // this up() migration is autogenerated, please modify it to your needs - $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + /** + * @param Schema $schema + * + * @return void + */ + public function up(Schema $schema) + { + // this up() migration is autogenerated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); - $this->addSql("CREATE TABLE digicomp_sequence_domain_model_insert (number INT NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(number, type)) ENGINE = InnoDB"); - } + $this->addSql( + "CREATE TABLE digicomp_sequence_domain_model_insert (number INT NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(number, type)) ENGINE = InnoDB" + ); + } - /** - * @param Schema $schema - * @return void - */ - public function down(Schema $schema) { - // this down() migration is autogenerated, please modify it to your needs - $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + /** + * @param Schema $schema + * + * @return void + */ + public function down(Schema $schema) + { + // this down() migration is autogenerated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); - $this->addSql("DROP TABLE digicomp_sequence_domain_model_insert"); - } -} \ No newline at end of file + $this->addSql("DROP TABLE digicomp_sequence_domain_model_insert"); + } +} diff --git a/composer.json b/composer.json index 9ba95e6..08889e8 100644 --- a/composer.json +++ b/composer.json @@ -1,38 +1,38 @@ { - "name": "digicomp/sequence", - "type": "typo3-flow-package", - "description": "", - "require": { - "typo3/flow": "~2.0|~3.0" - }, - "require-dev": { - "phpunit/phpunit": "3.7.*" - }, - "autoload": { - "psr-0": { - "DigiComp\\Sequence": "Classes" - } - }, - "extra": { - "branch-alias": { - "dev-master": "1.1.x-dev" - }, - "applied-flow-migrations": [ - "Inwebs.Basket-201409170938", - "TYPO3.FLOW3-201201261636", - "TYPO3.Fluid-201205031303", - "TYPO3.FLOW3-201205292145", - "TYPO3.FLOW3-201206271128", - "TYPO3.FLOW3-201209201112", - "TYPO3.Flow-201209251426", - "TYPO3.Flow-201211151101", - "TYPO3.Flow-201212051340", - "TYPO3.Flow-201310031523", - "TYPO3.Flow-201405111147", - "TYPO3.Fluid-20141113120800", - "TYPO3.Flow-20141113121400", - "TYPO3.Fluid-20141121091700", - "TYPO3.Fluid-20150214130800" - ] + "name": "digicomp/sequence", + "type": "typo3-flow-package", + "description": "", + "require": { + "typo3/flow": "~2.0|~3.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "autoload": { + "psr-0": { + "DigiComp\\Sequence": "Classes" } + }, + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + }, + "applied-flow-migrations": [ + "Inwebs.Basket-201409170938", + "TYPO3.FLOW3-201201261636", + "TYPO3.Fluid-201205031303", + "TYPO3.FLOW3-201205292145", + "TYPO3.FLOW3-201206271128", + "TYPO3.FLOW3-201209201112", + "TYPO3.Flow-201209251426", + "TYPO3.Flow-201211151101", + "TYPO3.Flow-201212051340", + "TYPO3.Flow-201310031523", + "TYPO3.Flow-201405111147", + "TYPO3.Fluid-20141113120800", + "TYPO3.Flow-20141113121400", + "TYPO3.Fluid-20141121091700", + "TYPO3.Fluid-20150214130800" + ] + } } \ No newline at end of file