Completed
Push — master ( 2bf349...d68558 )
by Anton
03:05
created

AbstractEnvelopeRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 4
1
<?php
2
3
namespace Covery\Client\Requests;
4
5
use Covery\Client\EnvelopeInterface;
6
use Covery\Client\TransportInterface;
7
use GuzzleHttp\Psr7\Request;
8
9
/**
10
 * Class AbstractEnvelopeRequest
11
 *
12
 * Base class for requests, built over Envelopes
13
 *
14
 * @package Covery\Client\Requests
15
 */
16
abstract class AbstractEnvelopeRequest extends Request
17
{
18
    public function __construct($apiPath, EnvelopeInterface $envelope)
19
    {
20
        if (!is_string($apiPath) || empty($apiPath)) {
21
            throw new \InvalidArgumentException('API path must be non-empty string');
22
        }
23
24
        // Building request
25
        $ids = array();
26
        foreach ($envelope->getIdentities() as $id) {
27
            $ids[] = $id->getType() . '=' . $id->getId();
28
        }
29
30
        $packet = array('type' => $envelope->getType(), 'sequence_id' => $envelope->getSequenceId());
31
32
        parent::__construct(
33
            'POST',
34
            TransportInterface::DEFAULT_URL . $apiPath,
35
            array(
36
                'X-Identities' => implode('&', $ids)
37
            ),
38
            json_encode(array_merge($packet, $envelope->toArray()))
39
        );
40
41
    }
42
}
43