Passed
Push — master ( 1fcf08...b6e803 )
by Evgenii
16:32 queued 13:25
created

DalliApiBody   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 46
ccs 17
cts 19
cp 0.8947
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A add() 0 8 2
A getAsXmlString() 0 3 1
1
<?php
2
3
4
namespace floor12\DalliApi\Models;
5
6
7
use ReflectionClass;
8
use ReflectionException;
9
use SimpleXMLElement;
10
use yii\base\ErrorException;
0 ignored issues
show
Bug introduced by
The type yii\base\ErrorException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class DalliApiBody extends BaseXmlObject
13
{
14
    /** @var string */
15
    protected $authToken;
16
    /** @var string */
17
    protected $apiMethodName = 'testMehod';
18
    /** @var SimpleXMLElement */
19
    protected $mainElement;
20
21 1
    public function __construct(?string $apiMethodName, ?string $authToken)
22
    {
23 1
        if (empty($apiMethodName))
24
            throw new ErrorException('Api method name is empty.');
25
26 1
        if (empty($authToken))
27
            throw new ErrorException('Auth token is empty.');
28
        
29 1
        $this->apiMethodName = $apiMethodName;
30 1
        $this->authToken = $authToken;
31 1
        $this->mainElement = new SimpleXMLElement("<$this->apiMethodName></$this->apiMethodName>");
32 1
        $auth = $this->mainElement->addChild('auth');
33 1
        $auth->addAttribute('token', $this->authToken);
34 1
    }
35
36
    /**
37
     * @param BaseXmlObject $object
38
     * @return $this
39
     * @throws ReflectionException
40
     */
41 1
    public function add(BaseXmlObject $object): self
42
    {
43 1
        $className = mb_strtolower((new ReflectionClass($object))->getShortName());
44 1
        $mainElement = $this->mainElement->addChild($className);
45 1
        foreach ($object as $attributeName => $attributeValue) {
46 1
            $this->processAttributeNameAndValue($mainElement, $attributeName, $attributeValue);
47
        }
48 1
        return $this;
49
    }
50
51
    /**
52
     * @param BaseXmlObject|null $object
53
     * @return string
54
     */
55 1
    public function getAsXmlString(BaseXmlObject $object = null): string
56
    {
57 1
        return $this->mainElement->asXML();
58
    }
59
}
60