Completed
Push — master ( f9ebf5...75079c )
by Oleg
32:28
created

ResponseFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 3
A getMessage() 0 8 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Stdlib\Validation;
5
6
use Psr\Http\Message\ResponseInterface;
7
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
8
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
9
use Zend\Diactoros\Response\JsonResponse;
10
11
final class ResponseFactory
12
{
13 138
    public function __invoke(
14
        string $message,
15
        $code,
16
        ?string $dataObjectName = null,
17
        $value = null,
18
        $count = null
19
    ): ResponseInterface {
20 138
        $data = [];
21 138
        if ($dataObjectName !== null) {
22 116
            $data[$dataObjectName] = $value;
23 116
            $data['validation'] = [];
24
        }
25 138
        if ($count !== null) {
26 38
            $data['count'] = $count;
27
        }
28 138
        return new JsonResponse([
29 138
            'data' => $data,
30 138
            'msg' => $this->getMessage($message, $code),
31 138
        ], $code);
32
    }
33
34 138
    private function getMessage($message, $code)
35
    {
36 138
        if ($code >= 200 and $code < 300) {
37 76
            return new SuccessMessage($message);
38
        } else {
39 64
            return new DangerMessage($message);
40
        }
41
    }
42
}
43