XOrder::fromFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * xOrder.
5
 *
6
 * @package     craftt/xorder-sdk
7
 * @author      Ryan Stratton <[email protected]>
8
 * @copyright   Copyright (c) Ryan Stratton
9
 * @license     https://github.com/craftt/xorder-php-sdk/blob/master/LICENSE.md Apache 2.0
10
 * @link        https://github.com/craftt/xorder-php-sdk
11
 */
12
13
namespace XOrder;
14
15
use XOrder\Contracts\XOrderInterface;
16
use XOrder\Exceptions\FileDoesNotExistException;
17
18
/**
19
 * XOrder
20
 */
21
class XOrder implements XOrderInterface
22
{
23
24
    /**
25
     * @var \SimpleXMLElement
26
     */
27
    public $xml;
28
29
    /**
30
     * Constructor
31
     *
32
     * To create an XOrder you can pass either a file path
33
     * or the xml as a string.  Just make sure to pass true
34
     * as the second argument when passing a file path.
35
     *
36
     * @param string  $xml
37
     * @param boolean $file
38
     */
39 10
    public function __construct($xml, $file = false)
40
    {
41 10
        if ($file) {
42 4
            $this->fromFile($xml);
43 2
        } else {
44 6
            $this->fromString($xml);
45
        }
46 8
    }
47
48
    /**
49
     * Get the string representation of the XML builder
50
     * object.
51
     *
52
     * @return string
53
     */
54 8
    public function getXML()
55
    {
56 8
        return $this->xml->saveXML();
57
    }
58
59
    /**
60
     * Load the xml order from file.
61
     *
62
     * @param  string $xml
63
     * @throws \XOrder\Exceptions\FileDoesNotExistException
64
     * @return \XOrder\XOrder
65
     */
66 4
    public function fromFile($xml)
67
    {
68 4
        if (!file_exists($xml)) {
69 2
            throw new FileDoesNotExistException('The xorder file does not exists.');
70
        }
71
72 2
        $this->xml = simplexml_load_file($xml);
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * load the xml order from string.
79
     *
80
     * @param  string $xml
81
     * @return \XOrder\XOrder
82
     */
83 6
    public function fromString($xml)
84
    {
85 6
        $this->xml = simplexml_load_string($xml);
86
87 6
        return $this;
88
    }
89
90
    /**
91
     * Get the string representation of the XML builder
92
     * object.
93
     *
94
     * @return string
95
     */
96 2
    public function __toString()
97
    {
98 2
        return $this->getXML();
99
    }
100
}
101