Failed Conditions
Push — master ( 3876bf...075658 )
by Florent
04:13
created

ClientConfigurationPutEndpoint::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\Endpoint\ClientConfiguration;
15
16
use Interop\Http\Factory\ResponseFactoryInterface;
17
use Interop\Http\ServerMiddleware\DelegateInterface;
18
use Interop\Http\ServerMiddleware\MiddlewareInterface;
19
use OAuth2Framework\Component\Server\Command\Client\UpdateClientCommand;
20
use OAuth2Framework\Component\Server\DataTransporter;
21
use OAuth2Framework\Component\Server\Model\DataBag\DataBag;
22
use OAuth2Framework\Component\Server\Response\OAuth2Exception;
23
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager;
24
use Psr\Http\Message\ServerRequestInterface;
25
use SimpleBus\Message\Bus\MessageBus;
26
27
final class ClientConfigurationPutEndpoint implements MiddlewareInterface
28
{
29
    /**
30
     * @var MessageBus
31
     */
32
    private $messageBus;
33
34
    /**
35
     * @var ResponseFactoryInterface
36
     */
37
    private $responseFactory;
38
39
    /**
40
     * ClientConfigurationPutEndpoint constructor.
41
     *
42
     * @param MessageBus               $messageBus
43
     * @param ResponseFactoryInterface $responseFactory
44
     */
45
    public function __construct(MessageBus $messageBus, ResponseFactoryInterface $responseFactory)
46
    {
47
        $this->messageBus = $messageBus;
48
        $this->responseFactory = $responseFactory;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function process(ServerRequestInterface $request, DelegateInterface $next)
55
    {
56
        $client = $request->getAttribute('client');
57
58
        $data = new DataTransporter();
59
        $command_parameters = DataBag::createFromArray(json_decode($request->getBody()->getContents(), true));
60
        $command = UpdateClientCommand::create($client, $command_parameters, $data);
61
62
        try {
63
            $this->messageBus->handle($command);
64
        } catch (\InvalidArgumentException $e) {
65
            throw new OAuth2Exception(400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_REQUEST, 'error_description' => $e->getMessage()]);
66
        }
67
68
        $response = $this->responseFactory->createResponse();
69
        $response->getBody()->write(json_encode($data->getData()->all()));
70
        $headers = ['Content-Type' => 'application/json; charset=UTF-8', 'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate, private', 'Pragma' => 'no-cache'];
71
        foreach ($headers as $k => $v) {
72
            $response = $response->withHeader($k, $v);
73
        }
74
75
        return $response;
76
    }
77
}
78