TASK: PSR-2 compliance

This commit is contained in:
Ferdinand Kuhl 2016-06-24 19:40:43 +02:00
parent 488e500c4d
commit 52f5040270
6 changed files with 226 additions and 196 deletions

View file

@ -6,7 +6,6 @@ namespace DigiComp\Sequence\Command;
* * * *
* */ * */
use Doctrine\DBAL\DBALException;
use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Annotations as Flow;
/** /**
@ -14,7 +13,8 @@ use TYPO3\Flow\Annotations as Flow;
* *
* @Flow\Scope("singleton") * @Flow\Scope("singleton")
*/ */
class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController { class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController
{
/** /**
* @var \DigiComp\Sequence\Service\SequenceGenerator * @var \DigiComp\Sequence\Service\SequenceGenerator
@ -28,7 +28,8 @@ class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController {
* @param int $to * @param int $to
* @param string $type * @param string $type
*/ */
public function advanceCommand($to, $type) { public function advanceCommand($to, $type)
{
$this->sequenceGenerator->advanceTo($to, $type); $this->sequenceGenerator->advanceTo($to, $type);
} }
} }

View file

@ -8,6 +8,7 @@ namespace DigiComp\Sequence\Domain\Model;
use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* SequenceInsert * SequenceInsert
* *
@ -15,7 +16,8 @@ use Doctrine\ORM\Mapping as ORM;
* @Flow\Scope("prototype") * @Flow\Scope("prototype")
* @Flow\Entity * @Flow\Entity
*/ */
class Insert { class Insert
{
/** /**
* @var int * @var int
@ -33,9 +35,10 @@ class Insert {
/** /**
* @param int $number * @param int $number
* @param string $type * @param string|object $type
*/ */
public function __construct($number, $type) { public function __construct($number, $type)
{
$this->setType($type); $this->setType($type);
$this->setNumber($number); $this->setNumber($number);
} }
@ -43,21 +46,24 @@ class Insert {
/** /**
* @param int $number * @param int $number
*/ */
public function setNumber($number) { public function setNumber($number)
{
$this->number = $number; $this->number = $number;
} }
/** /**
* @return int * @return int
*/ */
public function getNumber() { public function getNumber()
{
return $this->number; return $this->number;
} }
/** /**
* @param string|object $type * @param string|object $type
*/ */
public function setType($type) { public function setType($type)
{
if (is_object($type)) { if (is_object($type)) {
$type = get_class($type); $type = get_class($type);
} }
@ -67,7 +73,8 @@ class Insert {
/** /**
* @return string * @return string
*/ */
public function getType() { public function getType()
{
return $this->type; return $this->type;
} }
} }

View file

@ -7,9 +7,11 @@ namespace DigiComp\Sequence\Service;
* */ * */
use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Annotations as Flow;
/** /**
* SequenceException * SequenceException
*/ */
class Exception extends \Exception { class Exception extends \Exception
{
} }

View file

@ -10,21 +10,22 @@ use Doctrine\DBAL\DBALException;
use TYPO3\Flow\Annotations as Flow; 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, * Thoughts: We could make the step-range configurable, and if > 1 we could return new keys immediately for this
* as we "reserved" the space between. * request, as we "reserved" the space between.
* *
* @Flow\Scope("singleton") * @Flow\Scope("singleton")
*/ */
class SequenceGenerator { class SequenceGenerator
{
/** /**
* @var \Doctrine\Common\Persistence\ObjectManager * @var \Doctrine\Common\Persistence\ObjectManager
* @Flow\Inject * @Flow\Inject
*/ */
protected $_em; protected $entityManager;
/** /**
* @var \TYPO3\Flow\Reflection\ReflectionService * @var \TYPO3\Flow\Reflection\ReflectionService
@ -40,10 +41,12 @@ class SequenceGenerator {
/** /**
* @param string|object $type * @param string|object $type
*
* @throws \DigiComp\Sequence\Service\Exception * @throws \DigiComp\Sequence\Service\Exception
* @return int * @return int
*/ */
public function getNextNumberFor($type) { public function getNextNumberFor($type)
{
if (is_object($type)) { if (is_object($type)) {
$type = $this->reflectionService->getClassNameByObject($type); $type = $this->reflectionService->getClassNameByObject($type);
} }
@ -53,17 +56,22 @@ class SequenceGenerator {
$count = $this->getLastNumberFor($type); $count = $this->getLastNumberFor($type);
//TODO: Check for maximal tries, or similar //TODO: Check for maximal tries, or similar
//TODO: Let increment be configurable per type
do { do {
$count = $count+1; $count = $count + 1;
} while (! $this->validateFreeNumber($count, $type)); } while (!$this->validateFreeNumber($count, $type));
return $count; return $count;
} }
protected function validateFreeNumber($count, $type) { protected function validateFreeNumber($count, $type)
$em = $this->_em; {
$em = $this->entityManager;
/** @var $em \Doctrine\ORM\EntityManager */ /** @var $em \Doctrine\ORM\EntityManager */
try { 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; return true;
} catch (\PDOException $e) { } catch (\PDOException $e) {
return false; return false;
@ -79,22 +87,27 @@ class SequenceGenerator {
return false; return false;
} }
public function advanceTo($to, $type) { public function advanceTo($to, $type)
{
return ($this->validateFreeNumber($to, $type)); return ($this->validateFreeNumber($to, $type));
} }
/** /**
* @param $type * @param $type
*
* @return int * @return int
*/ */
public function getLastNumberFor($type) { public function getLastNumberFor($type)
{
/** @var $em \Doctrine\ORM\EntityManager */ /** @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 = $result->fetchAll();
$count = $count[0]['count']; $count = $count[0]['count'];
return $count; return $count;
} }
} }

View file

@ -7,24 +7,31 @@ use Doctrine\DBAL\Migrations\AbstractMigration,
/** /**
* Auto-generated Migration: Please modify to your need! * Auto-generated Migration: Please modify to your need!
*/ */
class Version20140505093853 extends AbstractMigration { class Version20140505093853 extends AbstractMigration
{
/** /**
* @param Schema $schema * @param Schema $schema
*
* @return void * @return void
*/ */
public function up(Schema $schema) { public function up(Schema $schema)
{
// this up() migration is autogenerated, please modify it to your needs // this up() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); $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 * @param Schema $schema
*
* @return void * @return void
*/ */
public function down(Schema $schema) { public function down(Schema $schema)
{
// this down() migration is autogenerated, please modify it to your needs // this down() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");