Passed
Push — master ( 3c19d1...2ca449 )
by Evgenii
09:11
created

DalliApiBody   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
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
11
class DalliApiBody extends BaseXmlObject
12
{
13
    /** @var string */
14
    protected $authToken;
15
    /** @var string */
16
    protected $apiMethodName = 'testMehod';
17
    /** @var SimpleXMLElement */
18
    protected $mainElement;
19
20 1
    public function __construct(string $apiMethodName, string $authToken)
21
    {
22 1
        $this->apiMethodName = $apiMethodName;
23 1
        $this->authToken = $authToken;
24 1
        $this->mainElement = new SimpleXMLElement("<$this->apiMethodName></$this->apiMethodName>");
25 1
        $auth = $this->mainElement->addChild('auth');
26 1
        $auth->addAttribute('token', $this->authToken);
27 1
    }
28
29
    /**
30
     * @param BaseXmlObject $object
31
     * @return $this
32
     * @throws ReflectionException
33
     */
34 1
    public function add(BaseXmlObject $object): self
35
    {
36 1
        $className = mb_strtolower((new ReflectionClass($object))->getShortName());
37 1
        $mainElement = $this->mainElement->addChild($className);
38 1
        foreach ($object as $attributeName => $attributeValue) {
39 1
            $this->processAttributeNameAndValue($mainElement, $attributeName, $attributeValue);
40
        }
41 1
        return $this;
42
    }
43
44
    /**
45
     * @param BaseXmlObject|null $object
46
     * @return string
47
     */
48 1
    public function getAsXmlString(BaseXmlObject $object = null): string
49
    {
50 1
        return $this->mainElement->asXML();
51
    }
52
}
53