XmlAuthenticationSuccessResponse::setAttributes()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 4
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: leo108
5
 * Date: 2016/10/23
6
 * Time: 16:01
7
 */
8
9
namespace Leo108\CAS\Responses;
10
11
use Leo108\CAS\Contracts\Responses\AuthenticationSuccessResponse;
12
use SimpleXMLElement;
13
14
class XmlAuthenticationSuccessResponse extends BaseXmlResponse implements AuthenticationSuccessResponse
15
{
16
    /**
17
     * XmlAuthenticationSuccessResponse constructor.
18
     */
19 5
    public function __construct()
20
    {
21 5
        parent::__construct();
22 5
        $this->node->addChild('cas:authenticationSuccess');
23 5
    }
24
25 1
    public function setUser($user)
26
    {
27 1
        $authNode = $this->getAuthNode();
28 1
        $this->removeByXPath($authNode, 'cas:user');
29 1
        $authNode->addChild('cas:user', $user);
30
31 1
        return $this;
32
    }
33
34 1
    public function setProxies($proxies)
35
    {
36 1
        $authNode = $this->getAuthNode();
37 1
        $this->removeByXPath($authNode, 'cas:proxies');
38 1
        $proxiesNode = $authNode->addChild('cas:proxies');
39 1
        foreach ($proxies as $proxy) {
40 1
            $proxiesNode->addChild('cas:proxy', $proxy);
41
        }
42
43 1
        return $this;
44
    }
45
46 2
    public function setAttributes($attributes)
47
    {
48 2
        $authNode = $this->getAuthNode();
49 2
        $this->removeByXPath($authNode, 'cas:attributes');
50 2
        $attributesNode = $authNode->addChild('cas:attributes');
51 2
        foreach ($attributes as $key => $value) {
52 2
            $valueArr = (array) $value;
53 2
            foreach($valueArr as $v){
54 2
                $str = $this->stringify($v);
55 2
                if (is_string($str)) {
56 2
                    $attributesNode->addChild('cas:'.$key, $str);
57
                }
58
            }
59
        }
60
61 2
        return $this;
62
    }
63
64 1
    public function setProxyGrantingTicket($ticket)
65
    {
66 1
        $authNode = $this->getAuthNode();
67 1
        $this->removeByXPath($authNode, 'cas:proxyGrantingTicket');
68 1
        $authNode->addChild('cas:proxyGrantingTicket', $ticket);
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @return SimpleXMLElement
75
     */
76 5 View Code Duplication
    protected function getAuthNode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78 5
        $authNodes = $this->node->xpath('cas:authenticationSuccess');
79 5
        if (count($authNodes) < 1) {
80
            return $this->node->addChild('cas:authenticationSuccess');
81
        }
82
83 5
        return $authNodes[0];
84
    }
85
}
86