1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wambo\Catalog; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use Wambo\Catalog\Exception\CatalogException; |
7
|
|
|
use Wambo\Catalog\Exception\JSONException; |
8
|
|
|
use Wambo\Catalog\Mapper\CatalogMapper; |
9
|
|
|
use Wambo\Catalog\Model\Catalog; |
10
|
|
|
use Wambo\Core\Storage\JSONDecoder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class JSONCatalogProvider reads product and catalog data from an JSON file. |
14
|
|
|
*/ |
15
|
|
|
class JSONCatalogProvider implements CatalogProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FilesystemInterface $filesystem The filesystem this Catalog instance works on |
19
|
|
|
*/ |
20
|
|
|
private $filesystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string $catalogFilePath The path to the JSON file containing the catalog in the given $filesystem |
24
|
|
|
*/ |
25
|
|
|
private $catalogFilePath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var CatalogMapper A catalog mapper for converting unstructured catalog data into Catalog models |
29
|
|
|
*/ |
30
|
|
|
private $catalogMapper; |
31
|
|
|
/** |
32
|
|
|
* @var JSONDecoder |
33
|
|
|
*/ |
34
|
|
|
private $jsonDecoder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new instance of the JSONCatalogProvider class. |
38
|
|
|
* |
39
|
|
|
* @param FilesystemInterface $filesystem The filesystem this Catalog instance works on |
40
|
|
|
* @param string $catalogFilePath The path to the JSON file containing the catalog in the given |
41
|
|
|
* $filesystem |
42
|
|
|
* |
43
|
|
|
* @param JSONDecoder $jsonDecoder A JSON decoder |
44
|
|
|
* @param CatalogMapper $catalogMapper A catalog mapper for converting unstructured catalog data into |
45
|
|
|
* Catalog models |
46
|
|
|
*/ |
47
|
5 |
|
public function __construct( |
48
|
|
|
FilesystemInterface $filesystem, |
49
|
|
|
string $catalogFilePath, |
50
|
|
|
JSONDecoder $jsonDecoder, |
51
|
|
|
CatalogMapper $catalogMapper |
52
|
|
|
) { |
53
|
5 |
|
$this->filesystem = $filesystem; |
54
|
5 |
|
$this->catalogFilePath = $catalogFilePath; |
55
|
5 |
|
$this->jsonDecoder = $jsonDecoder; |
56
|
5 |
|
$this->catalogMapper = $catalogMapper; |
57
|
5 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the product catalog. |
61
|
|
|
* |
62
|
|
|
* @return Catalog |
63
|
|
|
* |
64
|
|
|
* @throws CatalogException If the catalog could not be created. |
65
|
|
|
*/ |
66
|
5 |
|
public function getCatalog() |
67
|
|
|
{ |
68
|
5 |
|
if ($this->filesystem->has($this->catalogFilePath) == false) { |
|
|
|
|
69
|
1 |
|
throw new CatalogException(sprintf("The given catalog file path \"%s\" does not exist.", |
70
|
1 |
|
$this->catalogFilePath)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
|
75
|
4 |
|
$json = $this->filesystem->read($this->catalogFilePath); |
76
|
3 |
|
$catalogData = $this->jsonDecoder->getData($json); |
|
|
|
|
77
|
|
|
|
78
|
|
|
// convert the catalog data into a Catalog model |
79
|
2 |
|
$catalog = $this->catalogMapper->getCatalog($catalogData); |
80
|
|
|
|
81
|
1 |
|
return $catalog; |
82
|
|
|
|
83
|
3 |
|
} catch (\Exception $catalogException) { |
84
|
3 |
|
throw new CatalogException(sprintf("Unable to read catalog from %s: ", $this->catalogFilePath, |
85
|
3 |
|
$catalogException->getMessage()), $catalogException); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.