1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wambo\Catalog; |
4
|
|
|
|
5
|
|
|
use Wambo\Catalog\Exception\RepositoryException; |
6
|
|
|
use Wambo\Catalog\Mapper\ProductMapper; |
7
|
|
|
use Wambo\Catalog\Model\Product; |
8
|
|
|
use Wambo\Core\Storage\StorageInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ProductRepository fetches Product models from the Storage and writes Products back to the Storage |
12
|
|
|
* |
13
|
|
|
* @package Wambo\Catalog |
14
|
|
|
*/ |
15
|
|
|
class ProductRepository implements ProductRepositoryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var StorageInterface |
19
|
|
|
*/ |
20
|
|
|
private $productStorage; |
21
|
|
|
/** |
22
|
|
|
* @var ProductMapper |
23
|
|
|
*/ |
24
|
|
|
private $productMapper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates a new ProductRepository instance. |
28
|
|
|
* |
29
|
|
|
* @param StorageInterface $productStorage A product storage for reading and writing product data |
30
|
|
|
* @param ProductMapper $productMapper A product mapper for mapping unstructured data to Product models and |
31
|
|
|
* vice versa |
32
|
|
|
*/ |
33
|
3 |
|
public function __construct(StorageInterface $productStorage, ProductMapper $productMapper) |
34
|
|
|
{ |
35
|
3 |
|
$this->productStorage = $productStorage; |
36
|
3 |
|
$this->productMapper = $productMapper; |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get all products |
41
|
|
|
* |
42
|
|
|
* @return Product[] |
43
|
|
|
* |
44
|
|
|
* @throws RepositoryException If fetching the products failed |
45
|
|
|
*/ |
46
|
3 |
|
public function getProducts() |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
|
50
|
|
|
// read product data from storage |
51
|
3 |
|
$productDataArray = $this->productStorage->read(); |
52
|
|
|
|
53
|
|
|
// deserialize the product data |
54
|
2 |
|
$products = []; |
55
|
2 |
|
foreach ($productDataArray as $productData) { |
56
|
1 |
|
$products[] = $this->productMapper->getProduct($productData); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
return $products; |
60
|
|
|
|
61
|
1 |
|
} catch (\Exception $readException) { |
62
|
1 |
|
throw new RepositoryException("Failed to read products from storage provider.", $readException); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getById(string $id) |
67
|
|
|
{ |
68
|
|
|
throw new RepositoryException("Not implemented yet"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function add(Product $product) |
72
|
|
|
{ |
73
|
|
|
throw new RepositoryException("Not implemented yet"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function remove(Product $product) |
77
|
|
|
{ |
78
|
|
|
throw new RepositoryException("Not implemented yet"); |
79
|
|
|
} |
80
|
|
|
} |