Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

Completed
Pull Request — develop ( #41 )
by
unknown
27:26 queued 12:27
created

NormalizeArrayPlugin::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the bitbucket-api package.
5
 *
6
 * (c) Alexandru G. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bitbucket\API\Http\Plugin;
13
14
use Http\Client\Common\Plugin;
15
use Http\Discovery\MessageFactoryDiscovery;
16
use Http\Message\RequestFactory;
17
use Http\Promise\Promise;
18
use Psr\Http\Message\RequestInterface;
19
20
/**
21
 * Transform PHP array to API array
22
 *
23
 * PHP array square brackets does not play nice with remote API,
24
 * which expects just the key name, without brackets.
25
 *
26
 * Transforms: foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
27
 *
28
 * @author  Alexandru G.    <[email protected]>
29
 */
30
class NormalizeArrayPlugin implements Plugin
31
{
32
    /** @var \Http\Message\RequestFactory */
33
    private $requestFactory;
34
35
    public function __construct(RequestFactory $requestFactory = null)
36
    {
37
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
44
    {
45
        if (in_array('application/x-www-form-urlencoded', $request->getHeader('Content-Type'), true)) {
46
            return $next($request);
47
        }
48
49
        // Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
50
        $request = $this->requestFactory->createRequest(
51
            $request->getMethod(),
52
            $request->getUri(),
53
            $request->getHeaders(),
54
            preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getBody()->getContents()),
55
            $request->getProtocolVersion()
56
        );
57
58
        // Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
59
        $request = $request->withUri(
60
            $request->getUri()->withQuery(
61
                preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getUri()->getQuery())
62
            )
63
        );
64
65
        return $next($request);
66
    }
67
}
68