Skip to content

Commit

Permalink
fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerkrauss committed Feb 20, 2024
1 parent 201a8e9 commit bf4d28e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/Exceptions/BuyableNotEnoughStockException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

class BuyableNotEnoughStockException extends Exception
{
public function __construct($message = null, $code = 0, Exception $previous = null) {
if(!$message) {
public function __construct($message = null, $code = 0, Exception $previous = null)
{
if (!$message) {
$message = _t('BuyableNotEnoughStockException.OUT_OF_STOCK', 'This product does not have enough stock to fulfil your order');
}

Expand Down
6 changes: 3 additions & 3 deletions src/Extensions/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class OrderExtension extends DataExtension
{
public function beforeAdd($buyable, $quantity, $filter)
{
if(!$buyable->canPurchase(null, $quantity)) {
if (!$buyable->canPurchase(null, $quantity)) {
throw new BuyableNotEnoughStockException();
}
}

public function afterAdd($item, $buyable, $quantity, $filter)
{
if(!$buyable->canPurchase(null, $item->Quantity)) {
if (!$buyable->canPurchase(null, $item->Quantity)) {
throw new BuyableNotEnoughStockException();
}
}

public function beforeSetQuantity($buyable, $quantity, $filter)
{
if(!$buyable->canPurchase(null, $quantity)) {
if (!$buyable->canPurchase(null, $quantity)) {
throw new BuyableNotEnoughStockException();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/OrderItemExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function onPlacement()
$variation->decrementStock($this->owner);
}
}
} else if ($this->owner->ProductID) {
} elseif ($this->owner->ProductID) {
if ($product = $this->owner->Product()) {
if ($product->hasMethod('decrementStock')) {
$product->decrementStock($this->owner);
Expand Down
37 changes: 18 additions & 19 deletions src/Extensions/ProductStockExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public function updateCMSFields(FieldList $fields)
if ($this->hasVariations()) {
// it has variations so then we leave the management of the stock
// level to the variation.
$fields->addFieldToTab('Root.Stock', new LiteralField('StockManagedVariations',
'<p>You have variations attached to this product. To manage the stock level '.
$fields->addFieldToTab('Root.Stock', new LiteralField(
'StockManagedVariations',
'<p>You have variations attached to this product. To manage the stock level ' .
'click the Stock tab on each of the variations</p>'
));

Expand All @@ -50,7 +51,7 @@ public function updateCMSFields(FieldList $fields)

$grid = new GridField(
'StockLevels',
_t(__CLASS__ .'.Stock', 'Stock'),
_t(__CLASS__ . '.Stock', 'Stock'),
$this->getStockForEachWarehouse(),
GridFieldConfig::create()
->addComponent(new GridFieldButtonRow('before'))
Expand All @@ -59,16 +60,16 @@ public function updateCMSFields(FieldList $fields)
->addComponent(new GridFieldProductStockField())
);

$grid->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields(array(
'Title' => array(
$grid->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields([
'Title' => [
'field' => ReadonlyField::class
),
],
'Quantity' => function ($record, $column, $grid) {
// Numeric doesn't support null type
// return new NumericField($column);
return new TextField($column);
}
));
]);

// if the record has a root tab, (page) otherwise it could be a
// dataobject so we'll just
Expand Down Expand Up @@ -113,21 +114,20 @@ public function getStockForEachWarehouse()
*/
public function getStockForWarehouse(ProductWarehouse $warehouse, $strictCreate = true)
{
$record = $warehouse->StockedProducts()->filter(array(
$record = $warehouse->StockedProducts()->filter([
'ProductID'=> $this->owner->ID,
'ProductClass'=>$this->owner->ClassName
))->first();
])->first();

if (!$record && ($this->owner->isInDB() || !$strictCreate)) {

$defaults = ProductWarehouseStock::config()->get('defaults');
$record = Injector::inst()->create(ProductWarehouseStock::class);
$record->WarehouseID = $warehouse->ID;
$record->ProductID = $this->owner->ID;
$record->ProductClass = $this->owner->ClassName;
$record->Quantity = 0;

foreach($defaults as $field => $val){
foreach ($defaults as $field => $val) {
$record->{$field} = $val;
}

Expand Down Expand Up @@ -177,11 +177,11 @@ public function getTotalStockInCarts()
$current = ShoppingCart::curr();

$cartID = 0;
if( $current ){
if ($current) {
$cartID = $current->ID;
}

if($this->owner->isVariation()){
if ($this->owner->isVariation()) {
$identifier = "Variation";
$identifier2 = "ProductVariation";
} else {
Expand All @@ -191,22 +191,21 @@ public function getTotalStockInCarts()

$query = 'SELECT SUM(SilverShop_OrderItem.Quantity) AS QuantitySum
FROM SilverShop_OrderItem
LEFT JOIN SilverShop_'.$identifier.'_OrderItem ON SilverShop_'.$identifier.'_OrderItem.ID = SilverShop_OrderItem.ID
LEFT JOIN SilverShop_' . $identifier . '_OrderItem ON SilverShop_' . $identifier . '_OrderItem.ID = SilverShop_OrderItem.ID
LEFT JOIN SilverShop_OrderAttribute ON SilverShop_OrderAttribute.ID=SilverShop_OrderItem.ID
LEFT JOIN SilverShop_Order ON SilverShop_Order.ID=SilverShop_OrderAttribute.OrderID
WHERE SilverShop_'.$identifier.'_OrderItem.'.$identifier2.'ID = ' . $this->owner->ID . '
WHERE SilverShop_' . $identifier . '_OrderItem.' . $identifier2 . 'ID = ' . $this->owner->ID . '
AND SilverShop_Order.ID != ' . $cartID . '
AND SilverShop_Order.Status=\'Cart\'
GROUP BY SilverShop_'.$identifier.'_OrderItem.'.$identifier2.'ID';
GROUP BY SilverShop_' . $identifier . '_OrderItem.' . $identifier2 . 'ID';

$result = DB::query($query)->next();

if( $result ){
if ($result) {
return $result['QuantitySum'];
}

return 0;

}

/**
Expand Down Expand Up @@ -249,7 +248,7 @@ public function getWarehouseStockQuantity()
public function canPurchase($member = null, $quantity = 1)
{

if($this->getWarehouseStock()->count() < 1) {
if ($this->getWarehouseStock()->count() < 1) {
// no warehouses available.
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Model/ProductWarehouseStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function getTitle()
public function getBuyable()
{
if (is_null($this->_buyable)) {

if (!$this->ProductClass || !$this->ProductID) {
$buyable = false;
} else {
Expand All @@ -86,11 +85,12 @@ public function getBuyable()
/**
* Get the title for the buyable (product)
*/
public function getBuyableTitle() {
public function getBuyableTitle()
{

// This shouldn't happen... but if it does:
if (!$this->getBuyable()) {
return _t(__CLASS__.'.NOTITLE', '<unknown>');
return _t(__CLASS__ . '.NOTITLE', '<unknown>');
}

if ($this->getBuyable()->isVariation()) {
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/ShopStockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class ShopStockTest extends SapphireTest
private function setStockFor($item, $value)
{
$warehouse = $this->objFromFixture(ProductWarehouse::class, 'warehouse');
$data = array(
$data = [
'WarehouseID' => $warehouse->ID,
'ProductID' => $item->ID,
'ProductClass' => $item->getStockBaseIdentifier()
);
];

$stock = ProductWarehouseStock::get()->filter($data)->first();

Expand Down Expand Up @@ -61,17 +61,17 @@ public function testGetTotalStockInCarts()
{
$this->setStockFor($this->phone, 10);

$order = new Order(array(
$order = new Order([
'Status' => 'Cart'
));
]);

$order->write();

$orderItem = new OrderItem(array(
$orderItem = new OrderItem([
'ProductID' => $this->phone->ID,
'OrderID' => $order->ID,
'Quantity' => '5'
));
]);

$orderItem->write();
$this->assertEquals(5, $this->phone->getTotalStockInCarts());
Expand Down

0 comments on commit bf4d28e

Please sign in to comment.