SaveCookie::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Mcustiel\PowerRoute\Actions;
4
5
use Mcustiel\Mockable\DateTime;
6
use Mcustiel\PowerRoute\Common\TransactionData;
7
8
class SaveCookie implements ActionInterface
9
{
10
    use PlaceholderEvaluator;
11
12 1
    public function execute(TransactionData $transactionData, $argument = null)
13
    {
14 1
        $transactionData->setResponse(
15 1
            $transactionData->getResponse()->withHeader(
16 1
                'Set-Cookie',
17 1
                $this->buildSetCookieHeaderValue(
18 1
                    $argument,
19
                    $transactionData
20 1
                )
21 1
            )
22 1
        );
23 1
    }
24
25 1
    private function buildSetCookieHeaderValue($argument, $transactionData)
26
    {
27 1
        $value = $this->getValueOrPlaceholder($argument['value'], $transactionData);
28
29 1
        return $argument['name'] . '=' . $value . $this->getSetCookieDatePart($argument)
30 1
            . (isset($argument['domain']) ? '; domain=' . $argument['domain'] : '')
31 1
            . (isset($argument['path']) ? '; path=' . $argument['path'] : '')
32 1
            . (isset($argument['secure']) ? '; secure' : '');
33
    }
34
35 1
    private function getSetCookieDatePart($argument)
36
    {
37 1
        return isset($argument['ttl']) ? '; expires=' . date(
38 1
            DATE_COOKIE,
39 1
            ((new DateTime())->toPhpDateTime()->getTimestamp() + $argument['ttl'])
40 1
        ) : '';
41
    }
42
}
43