MessageService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
namespace OmnideskBundle\Service;
3
4
use GuzzleHttp\Exception\ClientException;
5
use OmnideskBundle\Exception\StaffNotActiveException;
6
use OmnideskBundle\Factory\Message\MessageConfigurationFactory;
7
use OmnideskBundle\Factory\Message\MessageDataTransformerFactory;
8
use OmnideskBundle\Request\Message\AddMessageRequest;
9
use OmnideskBundle\Request\Message\ListMessageRequest;
10
use OmnideskBundle\Response\Message\ListMessageResponse;
11
use OmnideskBundle\Response\Message\MessageResponse;
12
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
13
14
/**
15
 * Class MessageService
16
 * @package OmnideskBundle\Service
17
 */
18
class MessageService extends AbstractService
19
{
20
    /**
21
     * @var RequestService
22
     */
23
    protected $requestService;
24
25
    /**
26
     * @var MessageDataTransformerFactory
27
     */
28
    protected $transformerFactory;
29
30
    /**
31
     * @var MessageConfigurationFactory
32
     */
33
    protected $configurationFactory;
34
35
    /**
36
     * MessageService constructor.
37
     * @param RequestService                $requestService
38
     * @param MessageDataTransformerFactory $transformerFactory
39
     * @param MessageConfigurationFactory   $configurationFactory
40
     */
41
    public function __construct(
42
        RequestService $requestService,
43
        MessageDataTransformerFactory $transformerFactory,
44
        MessageConfigurationFactory $configurationFactory
45
    ) {
46
        $this->requestService = $requestService;
47
        $this->transformerFactory = $transformerFactory;
48
        $this->configurationFactory = $configurationFactory;
49
    }
50
51
    /**
52
     * @param AddMessageRequest $request
53
     * @return MessageResponse
54
     * @throws StaffNotActiveException
55
     */
56
    public function add(AddMessageRequest $request)
57
    {
58
        $transformer = $this->transformerFactory->get(MessageDataTransformerFactory::REQUEST_ADD);
59
        $configuration = $this->configurationFactory->get(MessageConfigurationFactory::CONFIGURATION_ADD);
60
61
        try {
62
            $params = $this->checkRequest($request, $transformer, $configuration);
63
        } catch (InvalidConfigurationException $exception) {
64
            throw new InvalidConfigurationException($exception->getMessage());
65
        }
66
67
        $transformer = $this->transformerFactory->get(MessageDataTransformerFactory::RESPONSE_VIEW);
68
69
        try {
70
            if (isset($params['attachments']) && !empty($params['attachments'])) {
71
                return $transformer->transform($this->requestService->postMultipart("cases/{$params['case_id']}/messages", 'message', $params));
72
            }
73
74
            return $transformer->transform($this->requestService->post("cases/{$params['case_id']}/messages", ['message' => $params]));
75
        } catch (ClientException $exception) {
76
            $contents = json_decode($exception->getResponse()->getBody(), JSON_UNESCAPED_UNICODE);
77
78
            if ($contents['error'] === MessageResponse::ERROR_STAFF_NOT_ACTIVE) {
79
                throw new StaffNotActiveException();
80
            }
81
82
            throw $exception;
83
        }
84
    }
85
86
    /**
87
     * @param ListMessageRequest $request
88
     * @return ListMessageResponse
89
     */
90 View Code Duplication
    public function lists(ListMessageRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $transformer = $this->transformerFactory->get(MessageDataTransformerFactory::REQUEST_LIST);
93
        $configuration = $this->configurationFactory->get(MessageConfigurationFactory::CONFIGURATION_LIST);
94
95
        try {
96
            $params = $this->checkRequest($request, $transformer, $configuration);
97
        } catch (InvalidConfigurationException $exception) {
98
            throw new InvalidConfigurationException($exception->getMessage());
99
        }
100
101
        $result = $this->requestService->get("cases/{$params['case_id']}/messages", $params);
102
103
        return $this->transformerFactory->get(MessageDataTransformerFactory::RESPONSE_LIST)->transform($result);
104
    }
105
}
106