ProductRepository.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Core\FacturisRepository;
  3. class ProductRepository
  4. extends Repository
  5. implements \Core\App\Repository\Facturis\ProductInterface
  6. {
  7. public $products;
  8. public function __construct($apiKey, $username, $password, $fiscalCode)
  9. {
  10. parent::__construct($apiKey, $username, $password, $fiscalCode);
  11. $this->products = $this->getProducts();
  12. }
  13. public function add(\Core\App\Entity\Product $product)
  14. {
  15. if($this->isNewProduct($product))
  16. {
  17. $product = (array)$product;
  18. $response = $this->insertProduct($product);
  19. if(isset($response['warning']) && $response['warning'] && isset($response['msg']))
  20. {
  21. throw new \Exception($response['msg']);
  22. }
  23. }
  24. }
  25. public function isNewProduct(\Core\App\Entity\Product $product)
  26. {
  27. if(!empty($product->getProdCod1()) && in_array($product->getProdCod1(), array_column($this->products, 'Alt_Cod')))
  28. return false;
  29. if(!empty($product->getProdCod()) && in_array($product->getProdCod(), array_column($this->products, 'Cod_EAN')))
  30. return false;
  31. if(!empty($product->getProdSku()) && in_array($product->getProdSku(), array_column($this->products, 'Cod_SKU')))
  32. return false;
  33. if(!empty($product->getProdNume()) && in_array($product->getProdNume(), array_column($this->products, 'Denumire')))
  34. return false;
  35. return true;
  36. }
  37. public function getAll()
  38. {
  39. $response = $this->getProducts();
  40. return $response;
  41. }
  42. }