Passed
Push — master ( 64594b...98afe9 )
by Michael
04:03
created

RigidIdentifierHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\IdentifierHandler;
5
6
/**
7
 * Identifier handler with rigidly set parameters for any objects
8
 *
9
 * @package Mikemirten\Component\JsonApi\ObjectTransformer\IdentifierHandler
10
 */
11
class RigidIdentifierHandler implements IdentifierHandlerInterface
12
{
13
    /**
14
     * Getter-method
15
     *
16
     * @var string
17
     */
18
    protected $getter;
19
20
    /**
21
     * Setter-method
22
     *
23
     * @var string
24
     */
25
    protected $setter;
26
27
    /**
28
     * RigidIdentifierHandler constructor.
29
     *
30
     * @param string $getter
31
     * @param string $setter Optional. Should not be set if "setIdentifier" have to be ignored
32
     */
33 2
    public function __construct(string $getter, string $setter = null)
34
    {
35 2
        $this->getter = $getter;
36 2
        $this->setter = $setter;
37 2
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function getIdentifier($object): string
43
    {
44 1
        return (string) $object->{$this->getter}();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function setIdentifier($object, string $identifier)
51
    {
52 1
        if ($this->setter === null) {
53
            return;
54
        }
55
56 1
        $object->{$this->setter}($identifier);
57
    }
58
}