Passed
Push — master ( 01a1a8...191f59 )
by Aleksandr
01:35
created

CommerceML::loadXml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php namespace Zenwalker\CommerceML;
2
3
use Zenwalker\CommerceML\Model\Catalog;
4
use Zenwalker\CommerceML\Model\Classifier;
5
use Zenwalker\CommerceML\Model\OfferPackage;
6
use Zenwalker\CommerceML\Model\Order;
7
use Zenwalker\CommerceML\Model\Product;
8
9
/**
10
 * Class CommerceML
11
 *
12
 * @package Zenwalker\CommerceML
13
 * @property Product[] $products
14
 */
15
class CommerceML
16
{
17
    public $classCatalog;
18
    public $classClassifier;
19
    public $classOfferPackage;
20
21
    /**
22
     * @var \SimpleXMLElement
23
     */
24
    public $importXml;
25
    /**
26
     * @var \SimpleXMLElement
27
     */
28
    public $offersXml;
29
    /**
30
     * @var \SimpleXMLElement
31
     */
32
    public $ordersXml;
33
    /**
34
     * @var Catalog
35
     */
36
    public $catalog;
37
    /**
38
     * @var Classifier
39
     */
40
    public $classifier;
41
42
    /**
43
     * @var OfferPackage
44
     */
45
    public $offerPackage;
46
47
    /**
48
     * @var Order
49
     */
50
    public $order;
51
52
    /**
53
     * Add XML files.
54
     *
55
     * @param string|bool $importXml
56
     * @param string|bool $offersXml
57
     * @param bool $ordersXml
58
     */
59 5
    public function addXmls($importXml = false, $offersXml = false, $ordersXml = false)
60
    {
61 5
        $this->loadImportXml($importXml);
62 5
        $this->loadOffersXml($offersXml);
63 5
        $this->loadOrdersXml($ordersXml);
64 5
    }
65
66
    /**
67
     * @param $file
68
     */
69 9
    public function loadImportXml($file)
70
    {
71 9
        $this->importXml = $this->loadXml($file);
72 9
        $this->catalog = new Catalog($this);
73 9
        $this->classifier = new Classifier($this);
74 9
    }
75
76
    /**
77
     * @param $file
78
     */
79 5
    public function loadOffersXml($file)
80
    {
81 5
        $this->offersXml = $this->loadXml($file);
82 5
        $this->offerPackage = new OfferPackage($this);
83 5
    }
84
85
    /**
86
     * @param $file
87
     */
88 7
    public function loadOrdersXml($file)
89
    {
90 7
        $this->ordersXml = $this->loadXml($file);
91 7
        $this->order = new Order($this);
92 7
    }
93
94
95
    /**
96
     * Load XML form file or string.
97
     *
98
     * @param string $xml
99
     *
100
     * @return \SimpleXMLElement
101
     */
102 11
    private function loadXml($xml)
103
    {
104 11
        if (is_file($xml)) {
105 7
            return simplexml_load_string(file_get_contents($xml));
0 ignored issues
show
Bug Best Practice introduced by
The expression return simplexml_load_st...ile_get_contents($xml)) could also return false which is incompatible with the documented return type SimpleXMLElement. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
106
        }
107
108 4
        return simplexml_load_string($xml);
0 ignored issues
show
Bug Best Practice introduced by
The expression return simplexml_load_string($xml) could also return false which is incompatible with the documented return type SimpleXMLElement. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
109
    }
110
}
111