Completed
Push — master ( 559524...927947 )
by Márk
08:24
created

OroContext::generateWsseAuthHeader()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 2
eloc 18
nc 2
nop 3
1
<?php
2
3
namespace Indigo\Oro\Behat\Context;
4
5
use Behat\Symfony2Extension\Context\KernelAwareContext;
6
use Behat\Symfony2Extension\Context\KernelDictionary;
7
use Oro\Bundle\SearchBundle\Engine\EngineInterface;
8
use Oro\Bundle\TestFrameworkBundle\Test\Client;
9
10
/**
11
 * Context containing Oro hooks.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15
class OroContext implements KernelAwareContext
16
{
17
    use KernelDictionary;
18
19
    /** Default WSSE credentials */
20
    const USER_NAME = 'admin';
21
    const USER_PASSWORD = 'admin_api_key';
22
23
    /**
24
     * @BeforeScenario @dbIsolation
25
     */
26
    public function startTransaction()
27
    {
28
        $client = $this->getClient();
29
30
        $client->startTransaction();
31
    }
32
33
    /**
34
     * @AfterScenario @dbIsolation
35
     */
36
    public function rollbackTransaction()
37
    {
38
        $client = $this->getClient();
39
40
        $client->rollbackTransaction();
41
    }
42
43
    /**
44
     * @BeforeScenario @dbIsolation&&@dbReindex
45
     */
46
    public function reindex()
47
    {
48
        /** @var EngineInterface $searchEngine */
49
        $searchEngine = $client = $this->getContainer()->get('oro_search.search.engine');
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
51
        $searchEngine->reindex();
52
    }
53
54
    /**
55
     * @BeforeScenario @wsse
56
     */
57
    public function setWsseHeader()
58
    {
59
        $client = $this->getClient();
60
61
        $client->setServerParameters($this->generateWsseAuthHeader());
62
    }
63
64
    /**
65
     * Returns the test client.
66
     *
67
     * @return Client
68
     */
69
    protected function getClient()
70
    {
71
        $client = $this->getContainer()->get('test.client');
72
73
        if (false === $client instanceof Client) {
74
            throw new \RuntimeException('The test client must be an instance of Oro\Bundle\TestFrameworkBundle\Test\Client');
75
        }
76
77
        return $client;
78
    }
79
80
    /**
81
     * Generates WSSE Auth header.
82
     *
83
     * {@link \Oro\Bundle\TestFrameworkBundle\Test\WebTestCase}
84
     *
85
     * @param string      $userName
86
     * @param string      $userPassword
87
     * @param string|null $nonce
88
     *
89
     * @return array
90
     */
91
    private function generateWsseAuthHeader(
92
        $userName = self::USER_NAME,
93
        $userPassword = self::USER_PASSWORD,
94
        $nonce = null
95
    ) {
96
        if (null === $nonce) {
97
            $nonce = uniqid();
98
        }
99
100
        $created  = date('c');
101
        $digest   = base64_encode(sha1(base64_decode($nonce) . $created . $userPassword, true));
102
        $wsseHeader = [
103
            'CONTENT_TYPE' => 'application/json',
104
            'HTTP_Authorization' => 'WSSE profile="UsernameToken"',
105
            'HTTP_X-WSSE' => sprintf(
106
                'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
107
                $userName,
108
                $digest,
109
                $nonce,
110
                $created
111
            )
112
        ];
113
114
        return $wsseHeader;
115
    }
116
}
117