Completed
Pull Request — dev (#11)
by
unknown
05:12 queued 01:17
created

JsonpEncoder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Vectorface\SnappyRouter\Encoder;
4
5
use \Exception;
6
use Vectorface\SnappyRouter\Response\AbstractResponse;
7
8
/**
9
 * Encodes the response in the JSON-P format.
10
 * @copyright Copyright (c) 2014, VectorFace, Inc.
11
 * @author Dan Bruce <[email protected]>
12
 */
13
class JsonpEncoder extends JsonEncoder
14
{
15
    /** the config key for the client side method to invoke */
16
    const KEY_CLIENT_METHOD = 'clientMethod';
17
18
    /** The method the client is invoking. */
19
    private $clientMethod;
20
21
    /**
22
     * Constructor for the encoder.
23
     * @param array $options (optional) The array of plugin options.
24
     */
25 2
    public function __construct($options = array())
26
    {
27 2
        parent::__construct($options);
28 2
        if (!isset($options[self::KEY_CLIENT_METHOD])) {
29 1
            throw new Exception('Client method missing from plugin options.');
30
        }
31 1
        $this->clientMethod = (string)$options[self::KEY_CLIENT_METHOD];
32 1
    }
33
34
    /**
35
     * @param AbstractResponse $response The response to be encoded.
36
     * @return Returns the response encoded in JSON.
37
     */
38 1
    public function encode(AbstractResponse $response)
39
    {
40 1
        return sprintf(
41 1
            '%s(%s);',
42 1
            $this->clientMethod,
43 1
            parent::encode($response)
44 1
        );
45
    }
46
}
47