Completed
Push — feature/evo-2707-username-code... ( 2c5f6c...8b4f51 )
by
unknown
12:16
created

CookieFieldStrategy::setDynamicParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
/**
3
 * authentification strategy based on a username cookie
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
/**
12
 * Class CookieFieldStrategy
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class CookieFieldStrategy extends AbstractHttpStrategy
19
{
20
    /** @var RequestStack */
21
    protected $requestStack;
22
23
    /** @var string  */
24
    protected $extractUsername;
25
26
    /** @var string  */
27
    protected $extractCoreId;
28
29
    /** @var string  */
30
    protected $clientIdName;
31
32
    /** @var String */
33
    protected $field;
34
35
    /**
36
     * @param String $field field
37
     */
38
    public function __construct($field)
39
    {
40
        $this->field = $field;
41
    }
42
43
    /**
44
     * Applies the defined strategy on the provided request.
45
     * Value may contain a coma separated string values, we use first as identifier.
46
     *
47
     * @param Request $request request to handle
48
     *
49
     * @return string
50
     */
51
    public function apply(Request $request)
52
    {
53
        $bagValue = $this->extractFieldInfo($request->cookies, $this->field);
54
55
        $pattern = "/((?m)(?<=\b{$this->extractUsername}=)[^,]*)/i";
56
        preg_match($pattern, $bagValue, $matches);
57
        if (!$matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            return $bagValue;
59
        }
60
        $fieldValue = $matches[0];
61
62
        if ($this->requestStack && $this->extractCoreId && $this->clientIdName) {
63
            $pattern = "/((?m)(?<=\b{$this->extractCoreId}=)[^,]*)/i";
64
            preg_match($pattern, $bagValue, $matches);
65
            if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
                /** @var Request $request */
67
                $request = $this->requestStack->getCurrentRequest();
68
                $request->attributes->set($this->clientIdName, $matches[0]);
69
            }
70
        }
71
72
        return $fieldValue;
73
    }
74
75
76
77
    /**
78
     * Symfony Container
79
     *
80
     * @param RequestStack $requestStack    request object
81
     * @param string       $extractUsername identifier in posted params
82
     * @param string       $extractCoreId   client specific identifier
83
     * @param string       $idName          save to request attrivute name
84
     * @return void
85
     */
86
    public function setDynamicParameters(RequestStack $requestStack, $extractUsername, $extractCoreId, $idName)
87
    {
88
        $this->requestStack = $requestStack;
89
        $this->extractUsername = $extractUsername;
90
        $this->extractCoreId = $extractCoreId;
91
        $this->clientIdName = $idName;
92
    }
93
}
94