TASK: PSR-2 compliance
This commit is contained in:
parent
488e500c4d
commit
52f5040270
6 changed files with 226 additions and 196 deletions
|
@ -6,7 +6,6 @@ namespace DigiComp\Sequence\Command;
|
|||
* *
|
||||
* */
|
||||
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
|
||||
/**
|
||||
|
@ -14,7 +13,8 @@ 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
|
||||
|
@ -28,7 +28,8 @@ class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController {
|
|||
* @param int $to
|
||||
* @param string $type
|
||||
*/
|
||||
public function advanceCommand($to, $type) {
|
||||
public function advanceCommand($to, $type)
|
||||
{
|
||||
$this->sequenceGenerator->advanceTo($to, $type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ namespace DigiComp\Sequence\Domain\Model;
|
|||
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* SequenceInsert
|
||||
*
|
||||
|
@ -15,7 +16,8 @@ use Doctrine\ORM\Mapping as ORM;
|
|||
* @Flow\Scope("prototype")
|
||||
* @Flow\Entity
|
||||
*/
|
||||
class Insert {
|
||||
class Insert
|
||||
{
|
||||
|
||||
/**
|
||||
* @var int
|
||||
|
@ -33,9 +35,10 @@ class Insert {
|
|||
|
||||
/**
|
||||
* @param int $number
|
||||
* @param string $type
|
||||
* @param string|object $type
|
||||
*/
|
||||
public function __construct($number, $type) {
|
||||
public function __construct($number, $type)
|
||||
{
|
||||
$this->setType($type);
|
||||
$this->setNumber($number);
|
||||
}
|
||||
|
@ -43,21 +46,24 @@ class Insert {
|
|||
/**
|
||||
* @param int $number
|
||||
*/
|
||||
public function setNumber($number) {
|
||||
public function setNumber($number)
|
||||
{
|
||||
$this->number = $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getNumber() {
|
||||
public function getNumber()
|
||||
{
|
||||
return $this->number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|object $type
|
||||
*/
|
||||
public function setType($type) {
|
||||
public function setType($type)
|
||||
{
|
||||
if (is_object($type)) {
|
||||
$type = get_class($type);
|
||||
}
|
||||
|
@ -67,7 +73,8 @@ class Insert {
|
|||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType() {
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
|
@ -7,9 +7,11 @@ namespace DigiComp\Sequence\Service;
|
|||
* */
|
||||
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
|
||||
/**
|
||||
* SequenceException
|
||||
*/
|
||||
class Exception extends \Exception {
|
||||
class Exception extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -10,21 +10,22 @@ 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;
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* @var \TYPO3\Flow\Reflection\ReflectionService
|
||||
|
@ -40,10 +41,12 @@ class SequenceGenerator {
|
|||
|
||||
/**
|
||||
* @param string|object $type
|
||||
*
|
||||
* @throws \DigiComp\Sequence\Service\Exception
|
||||
* @return int
|
||||
*/
|
||||
public function getNextNumberFor($type) {
|
||||
public function getNextNumberFor($type)
|
||||
{
|
||||
if (is_object($type)) {
|
||||
$type = $this->reflectionService->getClassNameByObject($type);
|
||||
}
|
||||
|
@ -53,17 +56,22 @@ class SequenceGenerator {
|
|||
$count = $this->getLastNumberFor($type);
|
||||
|
||||
//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;
|
||||
protected function validateFreeNumber($count, $type)
|
||||
{
|
||||
$em = $this->entityManager;
|
||||
/** @var $em \Doctrine\ORM\EntityManager */
|
||||
try {
|
||||
$em->getConnection()->insert('digicomp_sequence_domain_model_insert', array('number' => $count, 'type' => $type));
|
||||
$em->getConnection()->insert(
|
||||
'digicomp_sequence_domain_model_insert',
|
||||
['number' => $count, 'type' => $type]
|
||||
);
|
||||
return true;
|
||||
} catch (\PDOException $e) {
|
||||
return false;
|
||||
|
@ -79,22 +87,27 @@ class SequenceGenerator {
|
|||
return false;
|
||||
}
|
||||
|
||||
public function advanceTo($to, $type) {
|
||||
public function advanceTo($to, $type)
|
||||
{
|
||||
return ($this->validateFreeNumber($to, $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastNumberFor($type) {
|
||||
public function getLastNumberFor($type)
|
||||
{
|
||||
/** @var $em \Doctrine\ORM\EntityManager */
|
||||
$em = $this->_em;
|
||||
$em = $this->entityManager;
|
||||
|
||||
$result = $em->getConnection()->executeQuery('SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', array('type' => $type));
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
|
@ -7,24 +7,31 @@ use Doctrine\DBAL\Migrations\AbstractMigration,
|
|||
/**
|
||||
* 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) {
|
||||
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) {
|
||||
public function down(Schema $schema)
|
||||
{
|
||||
// this down() migration is autogenerated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
|
||||
|
||||
|
|
Loading…
Reference in a new issue