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

DalliApiBody::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
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