HandleAssertionsAction::doExecute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-IDP package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the GPL-3 license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Idp\Action\Profile\Outbound\Response;
13
14
use LightSaml\Action\ActionInterface;
15
use LightSaml\Action\CompositeActionInterface;
16
use LightSaml\Action\Profile\AbstractProfileAction;
17
use LightSaml\Context\Profile\AssertionContext;
18
use LightSaml\Context\Profile\Helper\LogHelper;
19
use LightSaml\Context\Profile\Helper\MessageContextHelper;
20
use LightSaml\Context\Profile\ProfileContext;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Foreach of the given assertion actions, creates an assertion context, executes it, and if it has an assertion,
25
 * adds it to the Response in outbound context.
26
 */
27
class HandleAssertionsAction extends AbstractProfileAction implements CompositeActionInterface
28
{
29
    /** @var ActionInterface[] */
30
    protected $assertionActions;
31
32
    /**
33
     * @param LoggerInterface   $logger
34
     * @param ActionInterface[] $assertionActions
35
     */
36
    public function __construct(LoggerInterface $logger, array $assertionActions = array())
37
    {
38
        parent::__construct($logger);
39
40
        foreach ($assertionActions as $action) {
41
            $this->add($action);
42
        }
43
    }
44
45
    /**
46
     * @param ActionInterface $assertionAction
47
     *
48
     * @return HandleAssertionsAction
49
     */
50
    public function add(ActionInterface $assertionAction)
51
    {
52
        $this->assertionActions[] = $assertionAction;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param callable $callable
59
     *
60
     * @return ActionInterface|null
61
     */
62
    public function map($callable)
63
    {
64
        foreach ($this->assertionActions as $k => $action) {
65
            $newAction = call_user_func($callable, $action);
66
            if ($newAction) {
67
                $this->assertionActions[$k] = $newAction;
68
            }
69
        }
70
    }
71
72
    /**
73
     * @param ProfileContext $context
74
     *
75
     * @return void
76
     */
77
    protected function doExecute(ProfileContext $context)
78
    {
79
        $response = MessageContextHelper::asResponse($context->getOutboundContext());
0 ignored issues
show
Bug introduced by
It seems like $context->getOutboundContext() can be null; however, asResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
81
        foreach ($this->assertionActions as $index => $action) {
82
            $name = sprintf('assertion_%s', $index);
83
            /** @var AssertionContext $assertionContext */
84
            $assertionContext = $context->getSubContext($name, AssertionContext::class);
85
            $assertionContext->setId($index);
86
87
            $action->execute($assertionContext);
88
89
            if ($assertionContext->getEncryptedAssertion()) {
90
                $response->addEncryptedAssertion($assertionContext->getEncryptedAssertion());
91
            } elseif ($assertionContext->getAssertion()) {
92
                $response->addAssertion($assertionContext->getAssertion());
93
            } else {
94
                $this->logger->warning('No assertion was built', LogHelper::getActionContext($context, $this));
95
            }
96
        }
97
    }
98
}
99