Completed
Pull Request — master (#1)
by Artem
01:15
created

decodeAndProcessResponseResult()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
/*
3
 * This file is part of the FreshCentrifugoBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\CentrifugoBundle\Service;
14
15
use Fresh\CentrifugoBundle\Exception\CentrifugoErrorException;
16
use Fresh\CentrifugoBundle\Exception\CentrifugoException;
17
use Fresh\CentrifugoBundle\Model\BatchRequest;
18
use Fresh\CentrifugoBundle\Model\CommandInterface;
19
use Fresh\CentrifugoBundle\Model\ResultableCommandInterface;
20
use Symfony\Contracts\HttpClient\ResponseInterface;
21
22
/**
23
 * ResponseProcessor.
24
 *
25
 * @author Artem Henvald <[email protected]>
26
 */
27
class ResponseProcessor
28
{
29
    /** @var CentrifugoChecker */
30
    private $centrifugoChecker;
31
32
    /**
33
     * @param CentrifugoChecker $centrifugoChecker
34
     */
35
    public function __construct(CentrifugoChecker $centrifugoChecker)
36
    {
37
        $this->centrifugoChecker = $centrifugoChecker;
38
    }
39
40
    /**
41
     * @param CommandInterface  $command
42
     * @param ResponseInterface $response
43
     *
44
     * @return array|null
45
     */
46
    public function processResponse(CommandInterface $command, ResponseInterface $response): ?array
47
    {
48
        $this->centrifugoChecker->assertValidResponseStatusCode($response);
49
        $this->centrifugoChecker->assertValidResponseHeaders($response);
50
        $this->centrifugoChecker->assertValidResponseContentType($response);
51
52
        $content = $response->getContent();
53
54
        if ($command instanceof BatchRequest) {
55
            $contents = \explode("\n", $content);
56
            $result = [];
57
            $commands = $command->getCommands();
58
59
            foreach ($contents as $innerContent) {
60
                $result[] = $this->decodeAndProcessResponseResult($commands->current(), $innerContent);
61
                $commands->next();
62
            }
63
64
            return $result;
65
        }
66
67
        return $this->decodeAndProcessResponseResult($command, $content);
68
    }
69
70
    /**
71
     * @param CommandInterface $command
72
     * @param string           $content
73
     *
74
     * @throws CentrifugoException
75
     * @throws CentrifugoErrorException
76
     *
77
     * @return array|null
78
     */
79
    private function decodeAndProcessResponseResult(CommandInterface $command, string $content): ?array
80
    {
81
        try {
82
            $data = \json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
83
        } catch (\Exception $e) {
84
            throw new CentrifugoException('Centrifugo response payload is not a valid JSON');
85
        }
86
87
        if (isset($data['error'])) {
88
            throw new CentrifugoErrorException($data['error']['message'], $data['error']['code']);
89
        }
90
91
        if ($command instanceof ResultableCommandInterface) {
92
            return $data['result'];
93
        }
94
95
        return null;
96
    }
97
}
98