RigidIdentifierHandler   A
last analyzed

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
use Mikemirten\Component\JsonApi\Mapper\MappingContext;
7
8
/**
9
 * Identifier handler with rigidly set parameters for any objects
10
 *
11
 * @package Mikemirten\Component\JsonApi\ObjectTransformer\IdentifierHandler
12
 */
13
class RigidIdentifierHandler implements IdentifierHandlerInterface
14
{
15
    /**
16
     * Getter-method
17
     *
18
     * @var string
19
     */
20
    protected $getter;
21
22
    /**
23
     * Setter-method
24
     *
25
     * @var string
26
     */
27
    protected $setter;
28
29
    /**
30
     * RigidIdentifierHandler constructor.
31
     *
32
     * @param string $getter
33
     * @param string $setter Optional. Should not be set if "setIdentifier" have to be ignored
34
     */
35 2
    public function __construct(string $getter, string $setter = null)
36
    {
37 2
        $this->getter = $getter;
38 2
        $this->setter = $setter;
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function getIdentifier($object, MappingContext $context): string
45
    {
46 1
        return (string) $object->{$this->getter}();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function setIdentifier($object, string $identifier, MappingContext $context)
53
    {
54 1
        if ($this->setter === null) {
55
            return;
56
        }
57
58 1
        $object->{$this->setter}($identifier);
59
    }
60
}