Skip to content

Commit

Permalink
Merge pull request #368 from tienvx/add-data-helper
Browse files Browse the repository at this point in the history
Add data helper
  • Loading branch information
tienvx committed Aug 22, 2019
2 parents 51ce4e2 + cd52715 commit fc44770
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 106 deletions.
32 changes: 32 additions & 0 deletions src/Helper/DataHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Helper;

use Exception;
use Tienvx\Bundle\MbtBundle\Entity\Data;

class DataHelper
{
/**
* @param Data $data
* @param string $key
* @param callable $miss
* @param callable $validate
*
* @return mixed
*
* @throws Exception
*/
public static function get(Data $data, string $key, callable $miss, callable $validate)
{
if ($data->has($key)) {
$value = $data->get($key);
$validate($value);
} else {
$value = $miss();
$data->set($key, $value);
}

return $value;
}
}
190 changes: 84 additions & 106 deletions tests/app/src/Subject/ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Tienvx\Bundle\MbtBundle\Annotation\Transition;
use Tienvx\Bundle\MbtBundle\Annotation\Place;
use Tienvx\Bundle\MbtBundle\Entity\Data;
use Tienvx\Bundle\MbtBundle\Helper\DataHelper;
use Tienvx\Bundle\MbtBundle\Subject\AbstractSubject;

class ShoppingCart extends AbstractSubject
Expand Down Expand Up @@ -143,15 +144,7 @@ public static function getName(): string
*/
public function viewAnyCategoryFromHome(Data $data)
{
if ($data->has('category')) {
$category = $data->get('category');
if (!in_array($category, $this->categories)) {
throw new Exception('Selected category is invalid');
}
} else {
$category = $this->categories[array_rand($this->categories)];
$data->set('category', $category);
}
$category = DataHelper::get($data, 'category', [$this, 'randomCategory'], [$this, 'validateCategory']);
$this->category = $category;
$this->product = null;
}
Expand All @@ -165,15 +158,7 @@ public function viewAnyCategoryFromHome(Data $data)
*/
public function viewOtherCategory(Data $data)
{
if ($data->has('category')) {
$category = $data->get('category');
if (!in_array($category, $this->categories)) {
throw new Exception('Selected category is invalid');
}
} else {
$category = $this->categories[array_rand($this->categories)];
$data->set('category', $category);
}
$category = DataHelper::get($data, 'category', [$this, 'randomCategory'], [$this, 'validateCategory']);
$this->category = $category;
$this->product = null;
}
Expand All @@ -187,15 +172,7 @@ public function viewOtherCategory(Data $data)
*/
public function viewAnyCategoryFromProduct(Data $data)
{
if ($data->has('category')) {
$category = $data->get('category');
if (!in_array($category, $this->categories)) {
throw new Exception('Selected category is invalid');
}
} else {
$category = $this->categories[array_rand($this->categories)];
$data->set('category', $category);
}
$category = DataHelper::get($data, 'category', [$this, 'randomCategory'], [$this, 'validateCategory']);
$this->category = $category;
$this->product = null;
}
Expand All @@ -209,15 +186,7 @@ public function viewAnyCategoryFromProduct(Data $data)
*/
public function viewAnyCategoryFromCart(Data $data)
{
if ($data->has('category')) {
$category = $data->get('category');
if (!in_array($category, $this->categories)) {
throw new Exception('Selected category is invalid');
}
} else {
$category = $this->categories[array_rand($this->categories)];
$data->set('category', $category);
}
$category = DataHelper::get($data, 'category', [$this, 'randomCategory'], [$this, 'validateCategory']);
$this->category = $category;
$this->product = null;
}
Expand All @@ -231,15 +200,7 @@ public function viewAnyCategoryFromCart(Data $data)
*/
public function viewProductFromHome(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (!in_array($product, $this->featuredProducts)) {
throw new Exception('Selected product is not in featured products list');
}
} else {
$product = $this->featuredProducts[array_rand($this->featuredProducts)];
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromHome'], [$this, 'validateProductFromHome']);
$this->product = $product;
$this->category = null;
}
Expand All @@ -253,16 +214,7 @@ public function viewProductFromHome(Data $data)
*/
public function viewProductFromCart(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (empty($this->cart[$product])) {
throw new Exception('Selected product is not in cart');
}
} else {
// This transition need this guard: subject.cartHasProducts()
$product = array_rand($this->cart);
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromCart'], [$this, 'validateProductFromCart']);
$this->product = $product;
$this->category = null;
}
Expand All @@ -276,17 +228,7 @@ public function viewProductFromCart(Data $data)
*/
public function viewProductFromCategory(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (!in_array($product, $this->productsInCategory[$this->category])) {
throw new Exception('Selected product is not in current category');
}
} else {
// This transition need this guard: subject.categoryHasProducts()
$products = $this->productsInCategory[$this->category] ?? [];
$product = $products[array_rand($products)];
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromCategory'], [$this, 'validateProductFromCategory']);
$this->product = $product;
$this->category = null;
}
Expand Down Expand Up @@ -432,15 +374,7 @@ public function backToHomeFromCheckout(Data $data)
*/
public function addFromHome(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (!in_array($product, $this->featuredProducts)) {
throw new Exception('Selected product is not in featured products list');
}
} else {
$product = $this->featuredProducts[array_rand($this->featuredProducts)];
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromHome'], [$this, 'validateProductFromHome']);
if (!$this->testingModel) {
if (in_array($product, $this->needOptions)) {
throw new Exception('You need to specify options for this product! Can not add product');
Expand All @@ -462,17 +396,7 @@ public function addFromHome(Data $data)
*/
public function addFromCategory(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (!in_array($product, $this->productsInCategory[$this->category])) {
throw new Exception('Selected product is not in current category');
}
} else {
// This transition need this guard: subject.categoryHasProducts()
$products = $this->productsInCategory[$this->category] ?? [];
$product = $products[array_rand($products)];
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromCategory'], [$this, 'validateProductFromCategory']);
if (!$this->testingModel) {
if (in_array($product, $this->needOptions)) {
throw new Exception('You need to specify options for this product! Can not add product');
Expand Down Expand Up @@ -515,16 +439,7 @@ public function addFromProduct(Data $data)
*/
public function remove(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (empty($this->cart[$product])) {
throw new Exception('Selected product is not in cart');
}
} else {
// This transition need this guard: subject.cartHasProducts()
$product = array_rand($this->cart);
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromCart'], [$this, 'validateProductFromCart']);
unset($this->cart[$product]);
}

Expand All @@ -537,16 +452,7 @@ public function remove(Data $data)
*/
public function update(Data $data)
{
if ($data->has('product')) {
$product = $data->get('product');
if (empty($this->cart[$product])) {
throw new Exception('Selected product is not in cart');
}
} else {
// This transition need this guard: subject.cartHasProducts()
$product = array_rand($this->cart);
$data->set('product', $product);
}
$product = DataHelper::get($data, 'product', [$this, 'randomProductFromCart'], [$this, 'validateProductFromCart']);
$this->cart[$product] = rand(1, 99);
}

Expand Down Expand Up @@ -641,4 +547,76 @@ public function getScreenshotUrl($bugId, $index)
{
return sprintf('http://localhost/mbt-api/bug-screenshot/%d/%d', $bugId, $index);
}

public function randomCategory()
{
return $this->categories[array_rand($this->categories)];
}

/**
* @param $category
*
* @throws Exception
*/
public function validateCategory($category)
{
if (!in_array($category, $this->categories)) {
throw new Exception('Selected category is invalid');
}
}

public function randomProductFromHome()
{
return $this->featuredProducts[array_rand($this->featuredProducts)];
}

/**
* @param $product
*
* @throws Exception
*/
public function validateProductFromHome($product)
{
if (!in_array($product, $this->featuredProducts)) {
throw new Exception('Selected product is not in featured products list');
}
}

public function randomProductFromCart()
{
// This transition need this guard: subject.cartHasProducts()
return array_rand($this->cart);
}

/**
* @param $product
*
* @throws Exception
*/
public function validateProductFromCart($product)
{
if (empty($this->cart[$product])) {
throw new Exception('Selected product is not in cart');
}
}

public function randomProductFromCategory()
{
// This transition need this guard: subject.categoryHasProducts()
$products = $this->productsInCategory[$this->category] ?? [];

return $products[array_rand($products)];
}

/**
* @param $product
*
* @throws Exception
*/
public function validateProductFromCategory($product)
{
if (!in_array($product, $this->productsInCategory[$this->category])) {
throw new Exception('Selected product is not in current category');
}
}
}

0 comments on commit fc44770

Please sign in to comment.