1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wambo\Catalog; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use Wambo\Catalog\Error\CatalogException; |
7
|
|
|
use Wambo\Catalog\Error\JSONException; |
8
|
|
|
use Wambo\Catalog\Mapper\CatalogMapper; |
9
|
|
|
use Wambo\Catalog\Model\Catalog; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class JSONCatalogProvider reads product and catalog data from an JSON file. |
13
|
|
|
*/ |
14
|
|
|
class JSONCatalogProvider implements CatalogProviderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var FilesystemInterface $filesystem The filesystem this Catalog instance works on |
18
|
|
|
*/ |
19
|
|
|
private $filesystem; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string $catalogFilePath The path to the JSON file containing the catalog in the given $filesystem |
23
|
|
|
*/ |
24
|
|
|
private $catalogFilePath; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CatalogMapper A catalog mapper for converting unstructured catalog data into Catalog models |
28
|
|
|
*/ |
29
|
|
|
private $catalogMapper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a new instance of the JSONCatalogProvider class. |
33
|
|
|
* |
34
|
|
|
* @param FilesystemInterface $filesystem The filesystem this Catalog instance works on |
35
|
|
|
* @param string $catalogFilePath The path to the JSON file containing the catalog in the given |
36
|
|
|
* $filesystem |
37
|
|
|
* |
38
|
|
|
* @param CatalogMapper $catalogMapper A catalog mapper for converting unstructured catalog data into |
39
|
|
|
* Catalog models |
40
|
|
|
*/ |
41
|
|
|
public function __construct(FilesystemInterface $filesystem, string $catalogFilePath, CatalogMapper $catalogMapper) |
42
|
|
|
{ |
43
|
|
|
$this->filesystem = $filesystem; |
44
|
|
|
$this->catalogFilePath = $catalogFilePath; |
45
|
|
|
$this->catalogMapper = $catalogMapper; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the product catalog. |
50
|
|
|
* |
51
|
|
|
* @return Catalog |
52
|
|
|
* |
53
|
|
|
* @throws CatalogException If the catalog could not be created. |
54
|
|
|
*/ |
55
|
|
|
public function getCatalog() |
56
|
|
|
{ |
57
|
|
|
if ($this->filesystem->has($this->catalogFilePath) == false) { |
|
|
|
|
58
|
|
|
return array(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
|
63
|
|
|
$json = $this->filesystem->read($this->catalogFilePath); |
64
|
|
|
$catalogData = $this->parseJSON($json); |
|
|
|
|
65
|
|
|
|
66
|
|
|
// convert the catalog data into a Catalog model |
67
|
|
|
$catalog = $this->catalogMapper->getCatalog($catalogData); |
68
|
|
|
|
69
|
|
|
return $catalog; |
70
|
|
|
|
71
|
|
|
} catch (\Exception $catalogException) { |
72
|
|
|
throw new CatalogException(sprintf("Unable to read catalog from %s: ", $this->catalogFilePath, |
73
|
|
|
$catalogException->getMessage()), $catalogException); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Parse the given JSON and convert it into an array. |
79
|
|
|
* |
80
|
|
|
* @param string $json JSON code |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
* |
84
|
|
|
* @throws JSONException If the given JSON could not be parsed |
85
|
|
|
*/ |
86
|
|
|
private function parseJSON($json) |
87
|
|
|
{ |
88
|
|
|
$catalogData = json_decode($json, true); |
89
|
|
|
|
90
|
|
|
// handle errors |
91
|
|
|
switch (json_last_error()) { |
92
|
|
|
|
93
|
|
|
case JSON_ERROR_DEPTH: |
94
|
|
|
throw new JSONException("The maximum stack depth has been exceeded"); |
95
|
|
|
|
96
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
97
|
|
|
throw new JSONException("Invalid or malformed JSON"); |
98
|
|
|
|
99
|
|
|
case JSON_ERROR_CTRL_CHAR: |
100
|
|
|
throw new JSONException("Control character error, possibly incorrectly encoded"); |
101
|
|
|
|
102
|
|
|
case JSON_ERROR_SYNTAX: |
103
|
|
|
throw new JSONException("Syntax error"); |
104
|
|
|
|
105
|
|
|
case JSON_ERROR_UTF8: |
106
|
|
|
throw new JSONException("Malformed UTF-8 characters, possibly incorrectly encoded"); |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $catalogData; |
111
|
|
|
} |
112
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: