Passed
Push — master ( a0632e...2294fc )
by Peter
03:03
created

Message::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Http\Controllers\Api;
6
7
use AbterPhp\Contact\Service\Execute\Message as MessageService;
8
use AbterPhp\Framework\Config\Provider as ConfigProvider;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Framework\Config\Provider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use AbterPhp\Framework\Http\Controllers\ApiDataTrait;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Framework\Http\Controllers\ApiDataTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use AbterPhp\Framework\Http\Controllers\ApiIssueTrait;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Framework\Http\Controllers\ApiIssueTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Opulence\Http\Responses\Response;
12
use Opulence\Http\Responses\ResponseHeaders;
13
use Opulence\Routing\Controller;
14
use Psr\Log\LoggerInterface;
15
16
class Message extends Controller
17
{
18
    use ApiIssueTrait;
19
    use ApiDataTrait;
20
21
    const ENTITY_SINGULAR = 'message';
22
    const ENTITY_PLURAL   = 'messages';
23
24
    const LOG_MSG_CREATE_FAILURE = 'Creating %1$s failed.';
25
26
    const LOG_CONTEXT_EXCEPTION  = 'Exception';
27
    const LOG_PREVIOUS_EXCEPTION = 'Previous exception #%d';
28
29
    /** @var LoggerInterface */
30
    protected $logger;
31
32
    /** @var MessageService */
33
    protected $messageService;
34
35
    /**
36
     * Message constructor.
37
     *
38
     * @param LoggerInterface $logger
39
     * @param MessageService  $messageService
40
     * @param ConfigProvider  $configProvider
41
     */
42
    public function __construct(
43
        LoggerInterface $logger,
44
        MessageService $messageService,
45
        ConfigProvider $configProvider
46
    ) {
47
        $this->logger         = $logger;
48
        $this->messageService = $messageService;
49
        $this->problemBaseUrl = $configProvider->getProblemBaseUrl();
0 ignored issues
show
Bug Best Practice introduced by
The property problemBaseUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
    }
51
52
    /**
53
     * @return Response
54
     */
55
    public function create(): Response
56
    {
57
        try {
58
            $data = $this->getCreateData();
59
60
            $formIdentifier = $data['form_id'];
61
62
            $errors = $this->messageService->validateForm($formIdentifier, $data);
63
64
            if (count($errors) > 0) {
65
                $msg = sprintf(static::LOG_MSG_CREATE_FAILURE, static::ENTITY_SINGULAR);
66
67
                return $this->handleErrors($msg, $errors);
68
            }
69
70
            $entity = $this->messageService->createEntity('');
71
            $entity = $this->messageService->fillEntity($formIdentifier, $entity, $data, []);
72
73
            $entity = $this->messageService->send($entity);
74
        } catch (\Exception $e) {
75
            $msg = sprintf(static::LOG_MSG_CREATE_FAILURE, static::ENTITY_SINGULAR);
76
77
            return $this->handleException($msg, $e);
78
        }
79
80
        return $this->handleCreateSuccess($entity);
0 ignored issues
show
Unused Code introduced by
The call to AbterPhp\Contact\Http\Co...::handleCreateSuccess() has too many arguments starting with $entity. ( Ignorable by Annotation )

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

80
        return $this->/** @scrutinizer ignore-call */ handleCreateSuccess($entity);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
    }
82
83
    /**
84
     * @return Response
85
     */
86
    protected function handleCreateSuccess(): Response
87
    {
88
        $response = new Response();
89
        $response->setStatusCode(ResponseHeaders::HTTP_NO_CONTENT);
90
91
        return $response;
92
    }
93
94
    /**
95
     * @param string $entityId
96
     *
97
     * @return Response
98
     */
99
    public function get(string $entityId): Response
100
    {
101
        return $this->handleNotImplemented();
102
    }
103
104
    /**
105
     * @return Response
106
     */
107
    public function list(): Response
108
    {
109
        return $this->handleNotImplemented();
110
    }
111
112
    /**
113
     * @param string $entityId
114
     *
115
     * @return Response
116
     */
117
    public function update(string $entityId): Response
118
    {
119
        return $this->handleNotImplemented();
120
    }
121
122
    /**
123
     * @param string $entityId
124
     *
125
     * @return Response
126
     */
127
    public function delete(string $entityId): Response
128
    {
129
        return $this->handleNotImplemented();
130
    }
131
}
132