Skip to content

Commit 4108e34

Browse files
committed
My suggestion
1 parent 6c142ca commit 4108e34

13 files changed

+278
-293
lines changed

AbstractCreateCalculationHandler.php

Lines changed: 0 additions & 155 deletions
This file was deleted.

CalculationCommand.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**
4+
* This class is used as date_class in form component
5+
*/
6+
class CalculationCommand
7+
{
8+
const BASIC_TYPE = 'basic_calculaction_type';
9+
10+
public $subTotal;
11+
public $quantity;
12+
}

CalculationController.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
class CalculationController
4+
{
5+
/**
6+
* @var CalculationHandler
7+
*/
8+
private $handler;
9+
10+
/**
11+
* @param CalculationHandler $handler
12+
*/
13+
public function __construct(CalculationHandler $handler) {
14+
$this->handler = $handler;
15+
}
16+
17+
/**
18+
* @Route("/calculation/{type}")
19+
* @Template()
20+
*/
21+
public function indexAction($type)
22+
{
23+
$command = $this->getFormData($type);
24+
25+
$this->handler->handle($command); // todo would probably be the command bus
26+
27+
// Get ID from entity and redirect, no return values required
28+
}
29+
30+
/**
31+
* @param string $type
32+
*
33+
* @return CalculationCommand
34+
*/
35+
private function getFormData($type)
36+
{
37+
// todo this could be a calculation mapper service
38+
$mapping = [
39+
CalculationCommand::BASIC_TYPE => CalculationCommand::class,
40+
];
41+
42+
// Form handling generates the configured command object
43+
/**
44+
* @var CalculationCommand $command
45+
*/
46+
$command = new $mapping[$type];
47+
$command->quantity = 12;
48+
$command->subTotal = 1000;
49+
50+
// Form would configure the command with values
51+
return $command;
52+
}
53+
}

CalculationHandler.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
class CalculationHandler
4+
{
5+
/**
6+
* @var EntityManager
7+
*/
8+
private $entityManager;
9+
10+
/**
11+
* @var TokenStorageInterface
12+
*/
13+
private $tokenStorage;
14+
15+
/**
16+
* @var Session
17+
*/
18+
private $session;
19+
20+
/**
21+
* @var Translator
22+
*/
23+
private $translator;
24+
25+
/**
26+
* @param EntityManager $entityManager
27+
* @param TokenStorageInterface $tokenStorage
28+
* @param Session $session
29+
* @param Translator $translator
30+
*
31+
* FIXME null values here are just to make prototype work
32+
*/
33+
public function __construct(
34+
EntityManager $entityManager,
35+
TokenStorageInterface $tokenStorage = null,
36+
Session $session = null,
37+
Translator $translator = null
38+
) {
39+
$this->entityManager = $entityManager;
40+
$this->tokenStorage = $tokenStorage;
41+
$this->session = $session;
42+
$this->translator = $translator;
43+
}
44+
45+
/**
46+
* @param CalculationCommand $command
47+
*/
48+
public function handle($command)
49+
{
50+
// transfer required args from command to construct. that way object is in valid state
51+
$entity = new DormerCalculation();// Command could contain necessary args for this object construct
52+
$entity->addPrice('total', $command->subTotal, $command->quantity);
53+
54+
$this->entityManager->persist($entity);
55+
$this->entityManager->flush();
56+
57+
// do other stuff with entity (translator etc.)
58+
}
59+
}

CreateDormerCalculation.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

CreateDormerCalculationHandler.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)