ResponseFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 5
c 5
b 0
f 3
lcom 1
cbo 2
dl 0
loc 84
ccs 27
cts 27
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B createResponse() 0 32 3
A checkExpectedValues() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Kickbox Bundle.
5
 *
6
 * (c) Abdoul Ndiaye <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Andi\KickBoxBundle\Factory;
13
14
use Andi\KickBoxBundle\Http\Response;
15
use Andi\KickBoxBundle\Exception\InvalidContentException;
16
17
/**
18
 * Kickbox Response Factory.
19
 *
20
 * @author Abdoul Ndiaye <[email protected]>
21
 */
22
class ResponseFactory
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $exceptedHeaders = [
28
        'X-Kickbox-Balance',
29
        'X-Kickbox-Response-Time',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    protected $expectedParameters = [
36
        'result',
37
        'reason',
38
        'role',
39
        'free',
40
        'disposable',
41
        'accept_all',
42
        'did_you_mean',
43
        'sendex',
44
        'email',
45
        'user',
46
        'domain',
47
        'success',
48
    ];
49
50
    /**
51
     * Create a KickBox Response according to an api call.
52
     *
53
     * @param array $headers    HTTP Headers of the call.
54
     * @param array $parameters Json Parameters of the call.
55
     *
56
     * @return Response A Kickbox response instance.
57
     */
58 4
    public function createResponse(array $headers, array $parameters)
59
    {
60 4
        $response = new Response();
61
62 4
        $this->checkExpectedValues($this->exceptedHeaders, $headers);
63
64 3
        if (isset($headers['X-Kickbox-Balance'][0])) {
65 3
            $response->setBalance($headers['X-Kickbox-Balance'][0]);
66 3
        }
67 3
        if (isset($headers['X-Kickbox-Response-Time'][0])) {
68 3
            $response->setResponseTime($headers['X-Kickbox-Response-Time'][0]);
69 3
        }
70
71 3
        $this->checkExpectedValues($this->expectedParameters, $parameters);
72
73
        $response
74 2
            ->setResult($parameters['result'])
75 2
            ->setReason($parameters['reason'])
76 2
            ->setRole($parameters['role'])
77 2
            ->setFree($parameters['free'])
78 2
            ->setDisposable($parameters['disposable'])
79 2
            ->setAcceptAll($parameters['accept_all'])
80 2
            ->setSuggestion($parameters['did_you_mean'])
81 2
            ->setSendex($parameters['sendex'])
82 2
            ->setEmail($parameters['email'])
83 2
            ->setUser($parameters['user'])
84 2
            ->setDomain($parameters['domain'])
85 2
            ->setSuccess($parameters['success'])
86
        ;
87
88 2
        return $response;
89
    }
90
91
    /**
92
     * Check if the expected values are in the array.
93
     *
94
     * @param $expectedValues
95
     * @param $array
96
     *
97
     * @return bool
98
     */
99 4
    protected function checkExpectedValues($expectedValues, $array)
100
    {
101 4
        if (count(array_intersect_key($array, array_flip($expectedValues))) != count($expectedValues)) {
102 2
            throw new InvalidContentException($expectedValues);
103
        }
104 3
    }
105
}
106