Passed
Push — master ( 279881...b4f44a )
by Divine Niiquaye
10:48
created

Helper::createRememberMeCookie()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
nc 7
nop 2
dl 0
loc 20
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security;
20
21
use Biurad\Http\Request;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Symfony\Component\HttpFoundation\Session\SessionInterface;
24
use Symfony\Component\PropertyAccess\Exception\AccessException;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27
28
/**
29
 * @author Divine Niiquaye Ibok <[email protected]>
30
 */
31
class Helper
32
{
33
    private static PropertyAccessorInterface $propertyAccessor;
34
35
    /**
36
     * Determine the targeted url from request, session or referer header.
37
     */
38
    public static function determineTargetUrl(
39
        ServerRequestInterface $request,
40
        SessionInterface $session = null,
41
        string $parameter = '_target_path',
42
        bool $fromReferer = false
43
    ): ?string {
44
        if ($targetUrl = self::getParameterValue($request, $parameter)) {
45
            return $targetUrl;
46
        }
47
48
        if (null === $session && ($request instanceof Request && $request->getRequest()->hasSession())) {
49
            $session = $request->getRequest()->getSession();
50
        }
51
52
        if (null !== $session && $targetUrl = $session->get($parameter)) {
53
            $session->remove($parameter);
54
        } elseif ($fromReferer && $targetUrl = $request->getHeaderLine('Referer')) {
55
            $pos = \strpos($targetUrl, '?');
56
57
            if (false !== $pos) {
58
                $targetUrl = \substr($targetUrl, 0, $pos);
59
            }
60
        }
61
62
        return $targetUrl ?: null;
63
    }
64
65
    /**
66
     * Returns a request "parameter" value.
67
     *
68
     * Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
69
     *
70
     * @param ServerRequestInterface|\stdClass $data
71
     *
72
     * @throws \InvalidArgumentException when the given path is malformed
73
     *
74
     * @return mixed
75
     */
76
    public static function getParameterValue(object $data, string $path, PropertyAccessorInterface $propertyAccessor = null)
77
    {
78
        if ($data instanceof ServerRequestInterface) {
79
            $getter = static function (string $value) use ($data) {
80
                if ($data instanceof Request) {
81
                    $requestedValue = $data->getRequest()->get($value);
82
                } else {
83
                    $requestedValue = $data->getAttributes()[$value] ?? $data->getQueryParams()[$value] ?? null;
84
                }
85
86
                if (null === $requestedValue) {
87
                    $data = (array) ($data->getParsedBody() ?? \json_decode(((string) $data->getBody()) ?: '', true));
88
                    $requestedValue = $data[$value] ?? null;
89
                }
90
91
                return $requestedValue;
92
            };
93
94
            if (false === $pos = \strpos($path, '[')) {
95
                return $getter($path);
96
            }
97
98
            if (null === $data = $getter(\substr($path, 0, $pos))) {
99
                return null;
100
            }
101
            $path = \substr($path, $pos);
102
        }
103
104
        try {
105
            self::$propertyAccessor ??= $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
106
107
            return self::$propertyAccessor->getValue($data, $path);
108
        } catch (AccessException $e) {
109
            return null;
110
        }
111
    }
112
113
    /**
114
     * Fetch the form data from the request.
115
     *
116
     * @param array<int,string> $parameterKeys
117
     *
118
     * @return array<int,mixed>
119
     */
120
    public static function getParameterValues(ServerRequestInterface $request, array $parameterKeys, PropertyAccessorInterface $propertyAccessor = null): array
121
    {
122
        if (empty($parameterKeys)) {
123
            return [];
124
        }
125
126
        foreach ($parameterKeys as $offset => $key) {
127
            unset($parameterKeys[$offset]);
128
            $parameterKeys[$key] = self::getParameterValue($request, $key, $propertyAccessor);
129
        }
130
131
        return $parameterKeys;
132
    }
133
}
134