Message   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 39
c 2
b 0
f 0
dl 0
loc 133
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleNotImplemented() 0 6 1
A list() 0 3 1
A handleCreateSuccess() 0 6 1
A update() 0 3 1
A create() 0 28 3
A get() 0 3 1
A delete() 0 3 1
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Http\Controllers\Api;
6
7
use AbterPhp\Admin\Constant\Env;
8
use AbterPhp\Admin\Http\Controllers\ApiDataTrait;
9
use AbterPhp\Admin\Http\Controllers\ApiIssueTrait;
10
use AbterPhp\Contact\Domain\Entities\Message as Entity;
11
use AbterPhp\Contact\Service\Execute\Message as MessageService;
12
use AbterPhp\Framework\Config\EnvReader;
13
use Opulence\Http\Responses\Response;
14
use Opulence\Http\Responses\ResponseHeaders;
15
use Opulence\Routing\Controller;
16
use Psr\Log\LoggerInterface;
17
18
class Message extends Controller
19
{
20
    use ApiIssueTrait;
21
    use ApiDataTrait;
22
23
    const ENTITY_SINGULAR = 'message';
24
    const ENTITY_PLURAL   = 'messages';
25
26
    const LOG_MSG_CREATE_FAILURE = 'Creating %1$s failed.';
27
28
    const LOG_CONTEXT_EXCEPTION  = 'Exception';
29
    const LOG_PREVIOUS_EXCEPTION = 'Previous exception #%d';
30
31
    /** @var LoggerInterface */
32
    protected $logger;
33
34
    /** @var MessageService */
35
    protected $messageService;
36
37
    /**
38
     * Message constructor.
39
     *
40
     * @param LoggerInterface $logger
41
     * @param MessageService  $messageService
42
     * @param EnvReader       $envReader
43
     */
44
    public function __construct(
45
        LoggerInterface $logger,
46
        MessageService $messageService,
47
        EnvReader $envReader
48
    ) {
49
        $this->logger         = $logger;
50
        $this->messageService = $messageService;
51
        $this->problemBaseUrl = $envReader->get(Env::API_PROBLEM_BASE_URL);
52
    }
53
54
    /**
55
     * @return Response
56
     */
57
    public function create(): Response
58
    {
59
        try {
60
            $data = $this->getCreateData();
61
62
            $formIdentifier = $data['form_id'];
63
64
            $errors = $this->messageService->validateForm($formIdentifier, $data);
65
66
            if (count($errors) > 0) {
67
                $msg = sprintf(static::LOG_MSG_CREATE_FAILURE, static::ENTITY_SINGULAR);
68
69
                return $this->handleErrors($msg, $errors);
70
            }
71
72
            $entity = $this->messageService->createEntity('');
73
            $entity = $this->messageService->fillEntity($formIdentifier, $entity, $data, []);
74
75
            assert($entity instanceof Entity, new \RuntimeException('Invalid entity.'));
76
77
            $this->messageService->send($entity);
78
        } catch (\Exception $e) {
79
            $msg = sprintf(static::LOG_MSG_CREATE_FAILURE, static::ENTITY_SINGULAR);
80
81
            return $this->handleException($msg, $e);
82
        }
83
84
        return $this->handleCreateSuccess();
85
    }
86
87
    /**
88
     * @return Response
89
     */
90
    protected function handleCreateSuccess(): Response
91
    {
92
        $response = new Response();
93
        $response->setStatusCode(ResponseHeaders::HTTP_NO_CONTENT);
94
95
        return $response;
96
    }
97
98
    /**
99
     * @return Response
100
     */
101
    protected function handleNotImplemented(): Response
102
    {
103
        $response = new Response();
104
        $response->setStatusCode(ResponseHeaders::HTTP_NOT_IMPLEMENTED);
105
106
        return $response;
107
    }
108
109
    /**
110
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
111
     *
112
     * @param string $entityId
113
     *
114
     * @return Response
115
     */
116
    public function get(string $entityId): Response
0 ignored issues
show
Unused Code introduced by
The parameter $entityId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function get(/** @scrutinizer ignore-unused */ string $entityId): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        return $this->handleNotImplemented();
119
    }
120
121
    /**
122
     * @return Response
123
     */
124
    public function list(): Response
125
    {
126
        return $this->handleNotImplemented();
127
    }
128
129
    /**
130
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
131
     *
132
     * @param string $entityId
133
     *
134
     * @return Response
135
     */
136
    public function update(string $entityId): Response
0 ignored issues
show
Unused Code introduced by
The parameter $entityId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

136
    public function update(/** @scrutinizer ignore-unused */ string $entityId): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        return $this->handleNotImplemented();
139
    }
140
141
    /**
142
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
143
     *
144
     * @param string $entityId
145
     *
146
     * @return Response
147
     */
148
    public function delete(string $entityId): Response
0 ignored issues
show
Unused Code introduced by
The parameter $entityId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
    public function delete(/** @scrutinizer ignore-unused */ string $entityId): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        return $this->handleNotImplemented();
151
    }
152
}
153