Skip to content

Commit

Permalink
fix type error when trying to get product id from object instead of a…
Browse files Browse the repository at this point in the history
…rray
  • Loading branch information
maxperei committed May 19, 2022
1 parent 8789b14 commit 37f1365
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@
{% endif %}
</div>
</div>

{% endblock %}
{% endblock %}
37 changes: 16 additions & 21 deletions src/Synolia/Bundle/StockAlertBundle/Twig/QuantityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\EntityManager;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\ProductBundle\Model\ProductView;
use Synolia\Bundle\StockAlertBundle\Entity\StockAlert;
use Synolia\Bundle\StockAlertBundle\Layout\DataProvider\InventoryQuantityDataProvider;
use Synolia\Bundle\StockAlertBundle\Layout\DataProvider\StockAlertDataProvider;
Expand All @@ -14,18 +15,9 @@

class QuantityExtension extends AbstractExtension
{
/**
* @var InventoryQuantityDataProvider
*/
protected $inventoryQuantityDataProvider;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var StockAlertDataProvider
*/
protected $stockAlertDataProvider;
protected InventoryQuantityDataProvider $inventoryQuantityDataProvider;
protected EntityManager $entityManager;
protected StockAlertDataProvider $stockAlertDataProvider;

public function __construct(
InventoryQuantityDataProvider $inventoryQuantityDataProvider,
Expand All @@ -37,7 +29,7 @@ public function __construct(
$this->stockAlertDataProvider = $stockAlertDataProvider;
}

public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction(
Expand All @@ -51,27 +43,30 @@ public function getFunctions()
];
}

public function getProductQuantity($product): int
public function getProductQuantity(mixed $product): int
{
$product = $this->getProductObjec($product);
$product = $this->getProductObj($product);
return $this->inventoryQuantityDataProvider->getAvailableQuantity($product);
}

public function productHasStockAlert($product): bool
public function productHasStockAlert(mixed $product): bool
{
$product = $this->getProductObjec($product);
$product = $this->getProductObj($product);
$stockAlert = $this->stockAlertDataProvider->getStockAlertForProduct($product);
if ($stockAlert instanceof StockAlert) {
return true;
}
return false;
}

protected function getProductObjec($product) : Product
protected function getProductObj(mixed $product): Product
{
if ($product instanceof Product) {
return $product;
if ($product instanceof ProductView) {
return $this->entityManager->getRepository(Product::class)->findOneBy(['id' => $product->getId()]);
}
return $this->entityManager->getRepository(Product::class)->findOneBy(['id' => $product['id']]);
if (is_array($product)) {
return $this->entityManager->getRepository(Product::class)->findOneBy(['id' => $product['id']]);
}
return $product;
}
}

0 comments on commit 37f1365

Please sign in to comment.