1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
<?php
/* --------------------------------------------------------------
ProductRepository.inc.php 2016-04-18
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class ProductRepository
*
* @category System
* @package Product
* @subpackage Repositories
*/
class ProductRepository implements ProductRepositoryInterface
{
/**
* @var ProductRepositoryReaderInterface
*/
protected $reader;
/**
* @var ProductRepositoryWriterInterface
*/
protected $writer;
/**
* @var ProductRepositoryDeleterInterface
*/
protected $deleter;
/**
* @var ProductSettingsRepositoryInterface
*/
protected $settingsRepo;
/**
* @var AddonValueServiceInterface
*/
protected $addonValueService;
/**
* @var ProductImageContainerRepositoryInterface
*/
protected $imageContainerRepo;
/**
* @var UrlRewriteStorage
*/
protected $urlRewriteStorage;
/**
* Initialize the product repository.
*
* @param ProductRepositoryReaderInterface $reader Instance to perform db read actions.
* @param ProductRepositoryWriterInterface $writer Instance to perform db write actions.
* @param ProductRepositoryDeleterInterface $deleter Instance to perform db delete actions.
* @param ProductSettingsRepositoryInterface $settingsRepo Repository instance to read/write/delete
* product settings.
* @param AddonValueServiceInterface $addonValueService Service to handle product addon values.
* @param ProductImageContainerRepositoryInterface $imageContainerRepo Image container of the product.
* @param UrlRewriteStorage $urlRewriteStorage Url rewrite storage.
*/
public function __construct(ProductRepositoryReaderInterface $reader,
ProductRepositoryWriterInterface $writer,
ProductRepositoryDeleterInterface $deleter,
ProductSettingsRepositoryInterface $settingsRepo,
AddonValueServiceInterface $addonValueService,
ProductImageContainerRepositoryInterface $imageContainerRepo,
UrlRewriteStorage $urlRewriteStorage)
{
$this->reader = $reader;
$this->writer = $writer;
$this->deleter = $deleter;
$this->settingsRepo = $settingsRepo;
$this->addonValueService = $addonValueService;
$this->imageContainerRepo = $imageContainerRepo;
$this->urlRewriteStorage = $urlRewriteStorage;
}
/**
* Adds a new product in the database.
*
* @param ProductInterface $product Product entity which holds the values for the database columns.
*
* @throws InvalidArgumentException If the provided product is not valid.
*
* @return int Id of inserted product.
*/
public function add(ProductInterface $product)
{
$productId = $this->writer->insert($product);
$productIdType = new IdType($productId);
$storedProduct = $this->reader->getById($productIdType);
$storedProduct->addAddonValues($product->getAddonValues());
$this->addonValueService->storeAddonValues($storedProduct);
$storedProduct->setSettings($product->getSettings());
$this->settingsRepo->store($productIdType, $storedProduct->getSettings());
$this->imageContainerRepo->store($productIdType, $product->getImageContainer());
$this->urlRewriteStorage->set($productIdType, $product->getUrlRewrites());
return $productId;
}
/**
* Updates an existing product in the database.
*
* @param StoredProductInterface $product Product entity to update.
*
* @throws InvalidArgumentException If the provided product is not valid.
*
* @return ProductRepositoryInterface|$this Same instance for chained method calls.
*/
public function store(StoredProductInterface $product)
{
$product->setLastModifiedDateTime(new DateTime());
$productSettings = $product->getSettings();
$productId = new IdType($product->getProductId());
$this->writer->update($product);
$this->addonValueService->storeAddonValues($product);
$this->settingsRepo->store($productId, $productSettings);
$this->imageContainerRepo->store($productId, $product->getImageContainer());
$this->urlRewriteStorage->set($productId, $product->getUrlRewrites());
return $this;
}
/**
* Gets a stored product by the given id.
*
* @param IdType $productId Id of expected product entity.
*
* @return StoredProductInterface Product entity with the expected product id.
*/
public function getProductById(IdType $productId)
{
$storedProduct = $this->reader->getById($productId);
$storedProduct->setSettings($this->settingsRepo->getProductSettingsById($productId));
$storedProduct->setImageContainer($this->imageContainerRepo->getByProductId($productId));
$storedProduct->setUrlRewrites($this->urlRewriteStorage->get($productId));
$this->addonValueService->loadAddonValues($storedProduct);
return $storedProduct;
}
/**
* Removes a product from the database by the given id.
*
* @param IdType $productId Id of expected product entity.
*
* @return ProductRepositoryInterface|$this Same instance for chained method calls.
*/
public function deleteProductById(IdType $productId)
{
$storedProduct = $this->reader->getById($productId);
$this->addonValueService->deleteAddonValues($storedProduct);
$this->deleter->deleteById($productId);
$this->imageContainerRepo->deleteByProductId($productId);
$this->urlRewriteStorage->delete($productId);
return $this;
}
}