-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOrdersController.php
64 lines (52 loc) · 1.77 KB
/
OrdersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace app\controllers;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use YiiDoctrineExample\Application\Command\EmitOrder;
use YiiDoctrineExample\Application\Command\EmitOrderHandler;
use YiiDoctrineExample\DomainModel\Order;
class OrdersController extends Controller
{
private EmitOrderHandler $emitOrderHandler;
private EntityManagerInterface $entityManager;
private SerializerInterface $serializer;
public function __construct(
$id,
$module,
EmitOrderHandler $emitOrderHandler,
EntityManagerInterface $entityManager,
SerializerInterface $serializer,
$config = [])
{
$this->emitOrderHandler = $emitOrderHandler;
$this->entityManager = $entityManager;
$this->serializer = $serializer;
parent::__construct($id, $module, $config);
}
public function actionEmit(): array
{
$this->entityManager->transactional(function() {
$command = new EmitOrder(15.6);
($this->emitOrderHandler)($command);
});
$response = \Yii::$app->response;
$response->statusCode = 201;
$response->format = Response::FORMAT_JSON;
return ['success' => true];
}
public function actionView(int $id): void
{
$order = $this->entityManager->find(Order::class, $id);
if (null === $order) {
throw new NotFoundHttpException;
}
$response = \Yii::$app->response;
$response->statusCode = 200;
$response->format = Response::FORMAT_JSON;
$response->content = $this->serializer->serialize($order, 'json');
}
}