Passed
Push — master ( 98afe9...62e274 )
by Michael
02:28
created

RigidIdentifierHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
wmc 4
lcom 2
cbo 0
ccs 10
cts 11
cp 0.9091
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getIdentifier() 0 4 1
A setIdentifier() 0 8 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Handler\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
}