Handler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Mediapart LaPresseLibre Bundle.
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/lapresselibre-bundle>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\Bundle\LaPresseLibreBundle;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
17
use Mediapart\Bundle\LaPresseLibreBundle\Factory\EndpointFactory;
18
use Mediapart\Bundle\LaPresseLibreBundle\Factory\TransactionFactory;
19
20
class Handler
21
{
22
    /**
23
     * @var EndpointFactory
24
     */
25
    private $endpointFactory;
26
27
    /**
28
     * @var TransactionFactory
29
     */
30
    private $transactionFactory;
31
32
    /**
33
     * @var DiactorosFactory
34
     */
35
    private $psr7Factory;
36
37
    /**
38
     * @param EndpointFactory $endpointFactory
39
     * @param TransactionFactory $transactionFactory
40
     * @param DiactorosFactory $psr7Factory
41
     */
42
    public function __construct(EndpointFactory $endpointFactory, TransactionFactory $transactionFactory, DiactorosFactory $psr7Factory)
43
    {
44
        $this->endpointFactory = $endpointFactory;
45
        $this->transactionFactory = $transactionFactory;
46
        $this->psr7Factory = $psr7Factory;
47
    }
48
49
    /**
50
     * @param Request $request
51
     * @return mixed Result of La Presse Libre transaction
52
     */
53
    public function process(Request $request)
54
    {
55
        $psrRequest = $this
56
            ->psr7Factory
57
            ->createRequest($request)
58
        ;
59
        $transaction = $this
60
            ->transactionFactory
61
            ->create($psrRequest)
62
        ;
63
        $endpoint = $this
64
            ->endpointFactory
65
            ->create($request->get('_route'))
66
        ;
67
68
        return $transaction->process($endpoint);
69
    }
70
71
    /**
72
     * @return array Required HTTP Response headers
73
     */
74
    public function getHttpResponseHeaders()
75
    {
76
        $publicKey = $this->transactionFactory->getPublicKey();
77
        $identity = $this->transactionFactory->getIdentity();
78
79
        return [
80
            'X-PART' => $publicKey,
81
            'X-LPL' => $identity->sign($publicKey),
82
            'X-TS' => $identity->getDatetime()->getTimestamp(),
83
        ];
84
    }
85
}
86