LaravelXmlServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 3
b 0
f 0
dl 0
loc 80
ccs 26
cts 27
cp 0.963
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A boot() 0 63 5
1
<?php
2
3
namespace Bmatovu\LaravelXml;
4
5
use Bmatovu\LaravelXml\Http\XmlResponse;
6
use Bmatovu\LaravelXml\Support\Facades\LaravelXml;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bmatovu\LaravelXml\LaravelXml. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Bmatovu\LaravelXml\Support\XmlElement;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Response;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\Support\Str;
12
13
class LaravelXmlServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap the application services.
17
     */
18 15
    public function boot(): void
19
    {
20 15
        if ($this->app->runningInConsole()) {
21 15
            $this->publishes([
22 15
                __DIR__.'/../config/xml.php' => config_path('xml.php'),
23 15
            ], 'config');
24
        }
25
26
        /*
27
         * Determine if the request is sending XML.
28
         * @deprecated v3.0.0
29
         *
30
         * @return bool
31
         */
32 15
        Request::macro('isXml', function () {
33 3
            return 'xml' === $this->getContentTypeFormat();
0 ignored issues
show
Bug introduced by
The method getContentTypeFormat() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

33
            return 'xml' === $this->/** @scrutinizer ignore-call */ getContentTypeFormat();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34 15
        });
35
36
        /*
37
         * Determine if the request is sending XML.
38
         *
39
         * @return bool
40
         */
41 15
        Request::macro('sentXml', function () {
42
            return 'xml' === $this->getContentTypeFormat();
43 15
        });
44
45
        /*
46
         * Get the XML payload for the request.
47
         *
48
         * @return \Bmatovu\LaravelXml\Support\XmlElement
49
         */
50 15
        Request::macro('xml', function () {
51 2
            if (! $this->isXml() || ! $content = $this->getContent()) {
0 ignored issues
show
Bug introduced by
The method isXml() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

51
            if (! $this->/** @scrutinizer ignore-call */ isXml() || ! $content = $this->getContent()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getContent() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

51
            if (! $this->isXml() || ! $content = $this->/** @scrutinizer ignore-call */ getContent()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52 1
                return new XmlElement('<document></document>');
53
            }
54
55 1
            return simplexml_load_string($content, XmlElement::class);
56 15
        });
57
58
        /*
59
         * Determine if the current request is asking for XML in return.
60
         *
61
         * @return bool
62
         */
63 15
        Request::macro('wantsXml', function () {
64 1
            $acceptable = $this->getAcceptableContentTypes();
0 ignored issues
show
Bug introduced by
The method getAcceptableContentTypes() does not exist on Bmatovu\LaravelXml\LaravelXmlServiceProvider. ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-call */ 
65
            $acceptable = $this->getAcceptableContentTypes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66 1
            return isset($acceptable[0]) && Str::contains($acceptable[0], ['/xml', '+xml']);
67 15
        });
68
69
        /*
70
         * Return a new XML response from the application.
71
         *
72
         * @param  string|array $data
73
         * @param  int $status
74
         * @param  array $headers
75
         * @param  array $options
76
         *
77
         * @return \Bmatovu\LaravelXml\Http\XmlResponse
78
         */
79 15
        Response::macro('xml', function ($data, $status = 200, array $headers = [], $options = []) {
80 2
            return new XmlResponse($data, $status, $headers, $options);
81 15
        });
82
    }
83
84
    /**
85
     * Register the application services.
86
     */
87 15
    public function register(): void
88
    {
89 15
        $this->mergeConfigFrom(__DIR__.'/../config/xml.php', 'xml');
90
91 15
        $this->app->bind('laravel-xml', function () {
92 8
            return new LaravelXml();
93 15
        });
94
    }
95
}
96