Passed
Push — develop ( b02061...820347 )
by Andreas
03:07
created

JSONCatalogProvider::parseJSON()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.8395

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 10
cts 14
cp 0.7143
rs 8.439
cc 6
eloc 14
nc 6
nop 1
crap 6.8395
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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);
0 ignored issues
show
Security Bug introduced by
It seems like $json defined by $this->filesystem->read($this->catalogFilePath) on line 75 can also be of type false; however, Wambo\Core\Storage\JSONDecoder::getData() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
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
}