commit 6b2c36c65dfb59061d5fa9d8bd3fc513a8d779c4 Author: Ferdinand Kuhl Date: Mon Apr 7 15:25:16 2014 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90ec22b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.svn diff --git a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php new file mode 100644 index 0000000..dc28358 --- /dev/null +++ b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php @@ -0,0 +1,34 @@ +sequenceGenerator->advanceTo($to, $type); + } +} diff --git a/Classes/DigiComp/Sequence/Domain/Model/Insert.php b/Classes/DigiComp/Sequence/Domain/Model/Insert.php new file mode 100644 index 0000000..7cad0b1 --- /dev/null +++ b/Classes/DigiComp/Sequence/Domain/Model/Insert.php @@ -0,0 +1,73 @@ +setType($type); + $this->setNumber($number); + } + + /** + * @param int $number + */ + public function setNumber($number) { + $this->number = $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; + } + + /** + * @return string + */ + public function getType() { + return $this->type; + } +} \ No newline at end of file diff --git a/Classes/DigiComp/Sequence/Service/Exception.php b/Classes/DigiComp/Sequence/Service/Exception.php new file mode 100644 index 0000000..ae9d6c3 --- /dev/null +++ b/Classes/DigiComp/Sequence/Service/Exception.php @@ -0,0 +1,15 @@ +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; + } + + 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; + } + + 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; + + $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 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0943c86 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name":"digicomp/sequence", + "type":"typo3-flow-package", + "description":"", + "require":{ + "typo3/flow":"~2.0" + }, + "require-dev":{ + "phpunit/phpunit": "3.7.*" + }, + "autoload":{ + "psr-0":{ + "DigiComp\\Sequence":"Classes" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} \ No newline at end of file