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,21 +13,23 @@ 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
* @Flow\Inject * @Flow\Inject
*/ */
protected $sequenceGenerator; protected $sequenceGenerator;
/** /**
* Sets minimum number for sequence generator * Sets minimum number for sequence generator
* *
* @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,59 +16,65 @@ use Doctrine\ORM\Mapping as ORM;
* @Flow\Scope("prototype") * @Flow\Scope("prototype")
* @Flow\Entity * @Flow\Entity
*/ */
class Insert { class Insert
{
/** /**
* @var int * @var int
* @ORM\Id * @ORM\Id
* @Flow\Identity * @Flow\Identity
*/ */
protected $number; protected $number;
/** /**
* @var string * @var string
* @ORM\Id * @ORM\Id
* @Flow\Identity * @Flow\Identity
*/ */
protected $type; protected $type;
/** /**
* @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->setNumber($number); $this->setType($type);
} $this->setNumber($number);
}
/** /**
* @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)) { {
$type = get_class($type); if (is_object($type)) {
} $type = get_class($type);
$this->type = $type; }
} $this->type = $type;
}
/** /**
* @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,91 +10,104 @@ 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
* @Flow\Inject * @Flow\Inject
*/ */
protected $reflectionService; protected $reflectionService;
/** /**
* @var \TYPO3\Flow\Log\SystemLoggerInterface * @var \TYPO3\Flow\Log\SystemLoggerInterface
* @Flow\Inject * @Flow\Inject
*/ */
protected $systemLogger; protected $systemLogger;
/** /**
* @param string|object $type * @param string|object $type
* @throws \DigiComp\Sequence\Service\Exception *
* @return int * @throws \DigiComp\Sequence\Service\Exception
*/ * @return int
public function getNextNumberFor($type) { */
if (is_object($type)) { public function getNextNumberFor($type)
$type = $this->reflectionService->getClassNameByObject($type); {
} if (is_object($type)) {
if (!$type) { $type = $this->reflectionService->getClassNameByObject($type);
throw new Exception('No Type given'); }
} if (!$type) {
$count = $this->getLastNumberFor($type); throw new Exception('No Type given');
}
$count = $this->getLastNumberFor($type);
//TODO: Check for maximal tries, or similar //TODO: Check for maximal tries, or similar
do { //TODO: Let increment be configurable per type
$count = $count+1; do {
} while (! $this->validateFreeNumber($count, $type)); $count = $count + 1;
return $count; } while (!$this->validateFreeNumber($count, $type));
} return $count;
}
protected function validateFreeNumber($count, $type) { protected function validateFreeNumber($count, $type)
$em = $this->_em; {
/** @var $em \Doctrine\ORM\EntityManager */ $em = $this->entityManager;
try { /** @var $em \Doctrine\ORM\EntityManager */
$em->getConnection()->insert('digicomp_sequence_domain_model_insert', array('number' => $count, 'type' => $type)); try {
return true; $em->getConnection()->insert(
} catch (\PDOException $e) { 'digicomp_sequence_domain_model_insert',
return false; ['number' => $count, 'type' => $type]
} catch (DBALException $e) { );
if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) { return true;
// Do nothing, new Doctrine handling hides the above error } catch (\PDOException $e) {
} else { return false;
$this->systemLogger->logException($e); } catch (DBALException $e) {
} if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) {
} catch (\Exception $e) { // Do nothing, new Doctrine handling hides the above error
$this->systemLogger->logException($e); } else {
} $this->systemLogger->logException($e);
return false; }
} } catch (\Exception $e) {
$this->systemLogger->logException($e);
}
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) { */
/** @var $em \Doctrine\ORM\EntityManager */ public function getLastNumberFor($type)
$em = $this->_em; {
/** @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)); $result = $em->getConnection()->executeQuery(
$count = $result->fetchAll(); 'SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type',
$count = $count[0]['count']; array('type' => $type)
return $count; );
} $count = $result->fetchAll();
$count = $count[0]['count'];
} return $count;
}
}

View file

@ -2,32 +2,39 @@
namespace TYPO3\Flow\Persistence\Doctrine\Migrations; namespace TYPO3\Flow\Persistence\Doctrine\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration, use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema; Doctrine\DBAL\Schema\Schema;
/** /**
* 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) { */
// this up() migration is autogenerated, please modify it to your needs public function up(Schema $schema)
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); {
// 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 * @param Schema $schema
* @return void *
*/ * @return void
public function down(Schema $schema) { */
// this down() migration is autogenerated, please modify it to your needs public function down(Schema $schema)
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); {
// 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"); $this->addSql("DROP TABLE digicomp_sequence_domain_model_insert");
} }
} }

View file

@ -1,38 +1,38 @@
{ {
"name": "digicomp/sequence", "name": "digicomp/sequence",
"type": "typo3-flow-package", "type": "typo3-flow-package",
"description": "", "description": "",
"require": { "require": {
"typo3/flow": "~2.0|~3.0" "typo3/flow": "~2.0|~3.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "3.7.*" "phpunit/phpunit": "3.7.*"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"DigiComp\\Sequence": "Classes" "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"
]
} }
},
"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"
]
}
} }