CreateMethod   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 24
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A createMethod() 0 15 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Rest/Traits/Methods/CreateMethod.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Rest\Traits\Methods;
10
11
use App\DTO\RestDtoInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Throwable;
15
16
/**
17
 * Trait CreateMethod
18
 *
19
 * @package App\Rest\Traits\Methods
20
 * @author TLe, Tarmo Leppänen <[email protected]>
21
 */
22
trait CreateMethod
23
{
24
    /**
25
     * Generic 'createMethod' method for REST resources.
26
     *
27
     * @param array<int, string>|null $allowedHttpMethods
28
     *
29
     * @throws Throwable
30
     */
31 29
    public function createMethod(
32
        Request $request,
33
        RestDtoInterface $restDto,
34
        ?array $allowedHttpMethods = null,
35
    ): Response {
36 29
        $resource = $this->getResourceForMethod($request, $allowedHttpMethods ?? [Request::METHOD_POST]);
0 ignored issues
show
Bug introduced by
It seems like getResourceForMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $resource = $this->getResourceForMethod($request, $allowedHttpMethods ?? [Request::METHOD_POST]);
Loading history...
37
38
        try {
39 20
            $data = $resource->create($restDto, true);
40
41 2
            return $this
42 2
                ->getResponseHandler()
0 ignored issues
show
Bug introduced by
It seems like getResponseHandler() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
                ->/** @scrutinizer ignore-call */ getResponseHandler()
Loading history...
43 2
                ->createResponse($request, $data, $resource, Response::HTTP_CREATED);
44 18
        } catch (Throwable $exception) {
45 18
            throw $this->handleRestMethodException($exception);
0 ignored issues
show
Bug introduced by
It seems like handleRestMethodException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

45
            throw $this->/** @scrutinizer ignore-call */ handleRestMethodException($exception);
Loading history...
46
        }
47
    }
48
}
49