Passed
Push — master ( 40f6c7...45ad77 )
by Brian
02:37
created

XmlResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 10
b 0
f 0
dl 0
loc 39
ccs 15
cts 16
cp 0.9375
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 5
1
<?php
2
3
namespace Bmatovu\LaravelXml\Http;
4
5
use Bmatovu\LaravelXml\Support\ArrayToXml;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\ResponseTrait;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Traits\Macroable;
10
use SimpleXMLElement;
11
use Symfony\Component\HttpFoundation\Response as BaseResponse;
12
13
class XmlResponse extends BaseResponse
14
{
15
    use Macroable, ResponseTrait {
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Bmatovu\LaravelXml\Http\XmlResponse.
Loading history...
Bug introduced by
The trait Illuminate\Http\ResponseTrait requires the property $callback which is not provided by Bmatovu\LaravelXml\Http\XmlResponse.
Loading history...
16
        Macroable::__call as macroCall;
17
    }
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param mixed $data The response data
23
     * @param int $status The response status code
24
     * @param array $headers An array of response headers
25
     * @param array $options
26
     */
27 2
    public function __construct($data = null, $status = 200, $headers = [], $options = [])
28
    {
29 2
        $headers = array_merge(config('xml.headers'), $headers);
30
31 2
        if ($data instanceof SimpleXmlElement) {
32 1
            parent::__construct($data->asXML(), $status, $headers);
0 ignored issues
show
Bug introduced by
It seems like $data->asXML() can also be of type true; however, parameter $content of Symfony\Component\HttpFo...Response::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            parent::__construct(/** @scrutinizer ignore-type */ $data->asXML(), $status, $headers);
Loading history...
33
34 1
            return;
35
        }
36
37 1
        if ($data instanceof Model || $data instanceof Collection) {
38
            $data = $data->toArray();
39
        }
40
41 1
        if (\is_array($data)) {
42 1
            $data = ArrayToXml::convert(
43 1
                $data,
44 1
                $options['root'] ?? config('xml.root'),
45 1
                $options['case'] ?? config('xml.case'),
46 1
                $options['declaration']['version'] ?? config('xml.declaration.version'),
47 1
                $options['declaration']['encoding'] ?? config('xml.declaration.encoding'),
48 1
            );
49
        }
50
51 1
        parent::__construct($data, $status, $headers);
52
    }
53
}
54