ApiActionTrait::setSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Majora\Framework\Domain\Action\Api;
4
5
use Majora\Framework\Api\Client\RestApiClient;
6
use Symfony\Component\Serializer\SerializerInterface;
7
8
/**
9
 * Base trait for api actions
10
 */
11
trait ApiActionTrait
12
{
13
    /**
14
     * @var RestApiClient
15
     */
16
    private $restClient;
17
18
    /**
19
     * @var SerializerInterface
20
     */
21
    private $serializer;
22
23
    /**
24
     * define rest client.
25
     *
26
     * @param RestApiClient $restClient
27
     */
28
    public function setRestApiClient(RestApiClient $restClient)
29
    {
30
        $this->restClient = $restClient;
31
    }
32
33
    /**
34
     * returns api client if defined
35
     *
36
     * @return RestApiClient
37
     */
38
    protected function getRestApiClient()
39
    {
40
        if (!$this->restClient) {
41
            throw new \BadMethodCallException(sprintf(
42
                'Method %s() cannot be used while rest api client is not configured.',
43
                __METHOD__
44
            ));
45
        }
46
47
        return $this->restClient;
48
    }
49
50
    /**
51
     * define serializer.
52
     *
53
     * @param SerializerInterface $serializer
54
     */
55
    public function setSerializer(SerializerInterface $serializer)
56
    {
57
        $this->serializer = $serializer;
58
    }
59
60
    /**
61
     * returns serializer if defined
62
     *
63
     * @return SerializerInterface
64
     */
65
    protected function getSerializer()
66
    {
67
        if (!$this->serializer) {
68
            throw new \BadMethodCallException(sprintf(
69
                'Method %s() cannot be used while serializer is not configured.',
70
                __METHOD__
71
            ));
72
        }
73
74
        return $this->serializer;
75
    }
76
}
77