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

AbstractEnvelopeRequest::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 13
nc 3
nop 2
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