mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-26 03:51:06 +01:00
Refactored function
This commit is contained in:
parent
0ace91c6a7
commit
560c70de38
@ -4,7 +4,11 @@ namespace repository\product;
|
|||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use repository\AbstractRepository;
|
use repository\AbstractRepository;
|
||||||
use entity\product\ProductInterface as ProductEntityInterface;
|
use entity\product\ProductInterface as ProductEntityInterface;
|
||||||
|
use entity\product\Product as ProductEntity;
|
||||||
use core\Core;
|
use core\Core;
|
||||||
|
use entity\currency\Euro;
|
||||||
|
use entity\price\Price;
|
||||||
|
use entity\image\UrlImage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -41,7 +45,29 @@ final class Product extends AbstractRepository implements ProductInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getAllProducts(): ArrayCollection
|
public function getAllProducts(): ArrayCollection
|
||||||
{}
|
{
|
||||||
|
$statement = $this->database->prepare('SELECT * FROM '.self::TABLE.';');
|
||||||
|
$statement->execute();
|
||||||
|
foreach ($statement->fetchAll() as $product){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function createProduct(string $name, string $color, int $cents, int $tax, string $imagePath):ProductEntity{
|
||||||
|
$product = new ProductEntity();
|
||||||
|
$product->setName($name);
|
||||||
|
$product->setColor($color);
|
||||||
|
$euro = new Euro();
|
||||||
|
$euro->setCents($cents);
|
||||||
|
$price = new Price();
|
||||||
|
$price->setPrice($euro);
|
||||||
|
$price->setTax($tax);
|
||||||
|
$product->setPrice($price);
|
||||||
|
$image = new UrlImage();
|
||||||
|
$image->setImage($imagePath);
|
||||||
|
$product->setImage($image);
|
||||||
|
return $product;
|
||||||
|
}
|
||||||
|
|
||||||
public function deleteAllProducts(): void
|
public function deleteAllProducts(): void
|
||||||
{}
|
{}
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use core\Core;
|
use core\Core;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use entity\product\Product;
|
|
||||||
use entity\currency\Euro;
|
|
||||||
use entity\price\Price;
|
|
||||||
use entity\image\UrlImage;
|
|
||||||
use repository\product\Product as ProductRepository;
|
use repository\product\Product as ProductRepository;
|
||||||
|
|
||||||
require __DIR__ . '/../vendor/autoload.php';
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
@ -38,18 +34,12 @@ $lines = explode("\n", trim(file_get_contents(__DIR__ . '/monking/product.csv'))
|
|||||||
unset($lines[0]);
|
unset($lines[0]);
|
||||||
foreach ($lines as $number=>$line) {
|
foreach ($lines as $number=>$line) {
|
||||||
$colums = explode(',', $line);
|
$colums = explode(',', $line);
|
||||||
$product = new Product();
|
$product = ProductRepository::createProduct(
|
||||||
$product->setName($colums[0]);
|
$colums[0],
|
||||||
$product->setColor($colums[1]);
|
$colums[1],
|
||||||
$euro = new Euro();
|
(int) (floatval($colums[2]) * 100),
|
||||||
$euro->setCents((int) (floatval($colums[2]) * 100));
|
(int)$colums[3],
|
||||||
$price = new Price();
|
$colums[4]);
|
||||||
$price->setPrice($euro);
|
|
||||||
$price->setTax((int)$colums[3]);
|
|
||||||
$product->setPrice($price);
|
|
||||||
$image = new UrlImage();
|
|
||||||
$image->setImage($colums[4]);
|
|
||||||
$product->setImage($image);
|
|
||||||
echo $number.'. product '.$product->getName()." added to collection...\n";
|
echo $number.'. product '.$product->getName()." added to collection...\n";
|
||||||
$products->add($product);
|
$products->add($product);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user