In between commit implementing order repository

This commit is contained in:
Kevin Frantz 2018-07-15 15:40:00 +02:00
parent 3fdcd47e9c
commit aa18f2b18e
3 changed files with 45 additions and 6 deletions

View File

@ -7,6 +7,8 @@ use repository\order\OrderInterface as OrderRepositoryInterface;
use repository\product\ProductInterface as ProductRepositoryInterface; use repository\product\ProductInterface as ProductRepositoryInterface;
use repository\product\Product as ProductRepository; use repository\product\Product as ProductRepository;
use entity\payment\AbstractPayment; use entity\payment\AbstractPayment;
use repository\order\Order as OrderRepository;
use entity\order\Order as OrderEntity;
/** /**
* *
@ -32,6 +34,7 @@ final class Order extends AbstractDefaultController implements OrderInterface
{ {
parent::__construct($core); parent::__construct($core);
$this->productRepository = new ProductRepository($this->core); $this->productRepository = new ProductRepository($this->core);
$this->orderRepository = new OrderRepository($this->core);
} }
private function addProduct(): void private function addProduct(): void
@ -58,6 +61,12 @@ final class Order extends AbstractDefaultController implements OrderInterface
if ($this->post['add']) { if ($this->post['add']) {
$this->addProduct(); $this->addProduct();
} }
if ($this->post['store']){
$this->core->getBasket()->setCustomer($this->core->getUser());
if($this->orderRepository->saveOrder($this->core->getBasket())){
$this->core->setBasket(new OrderEntity());
}
}
} }
} }

View File

@ -0,0 +1,35 @@
<?php
namespace repository\order;
use repository\AbstractRepository;
use entity\order\OrderInterface as OrderEntityInterface;
use core\CoreInterface;
/**
*
* @author kevinfrantz
*
*/
final class Order extends AbstractRepository implements OrderInterface
{
public function saveOrder(OrderEntityInterface $order): bool
{
$this->database->beginTransaction();
$this->saveOrderEntity($order);
$this->saveConnection($order);
return $this->database->rollBack();
}
private function saveOrderEntity(OrderEntityInterface $order):void{
$statement = $this->database->prepare("INSERT INTO `order` (`customer`) VALUES (?);");
$statement->execute([$order->getCustomer()->getId()]);
$order->setId($this->database->lastInsertId());
}
private function saveConnection(OrderEntityInterface $order):void{
foreach ($order->getProducts()->toArray() as $product){
$statement = $this->database->prepare("INSERT INTO `order_product` (`product_id`,`order_id`) VALUES (?,?);");
$statement->execute([$product->getId(),$order->getId()]);
}
}
}

View File

@ -10,11 +10,6 @@ use entity\order\OrderInterface as OrderEntityInterface;
*/ */
interface OrderInterface interface OrderInterface
{ {
public function saveOrder(OrderEntityInterface $order):void; public function saveOrder(OrderEntityInterface $order):bool;
/**
* This function just exists for maintaining reasons
*/
public function deleteAllOrders():void;
} }