Completed
Push — master ( 28d89f...74b41a )
by Dominik
02:30
created

LazyModelReference::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Reference;
6
7
use Chubbyphp\Model\ModelInterface;
8
use Chubbyphp\Model\ResolverInterface;
9
10
final class LazyModelReference implements ModelReferenceInterface
11
{
12
    /**
13
     * @var ResolverInterface
14
     */
15
    private $resolver;
16
17
    /**
18
     * @var string
19
     */
20
    private $modelClass;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $id;
26
27
    /**
28
     * @var bool
29
     */
30
    private $resolved = false;
31
32
    /**
33
     * @var ModelInterface|null
34
     */
35
    private $initialModel;
36
37
    /**
38
     * @var ModelInterface|null
39
     */
40
    private $model;
41
42
    /**
43
     * @param ResolverInterface $resolver
44
     * @param string $modelClass
45
     * @param string|null $id
46
     */
47 5
    public function __construct(ResolverInterface $resolver, string $modelClass, string $id = null)
48
    {
49 5
        $this->resolver = $resolver;
50 5
        $this->modelClass = $modelClass;
51 5
        $this->id = $id;
52 5
    }
53
54 5
    private function resolveModel()
55
    {
56 5
        if ($this->resolved) {
57 4
            return;
58
        }
59
60 5
        $this->resolved = true;
61
62 5
        $this->initialModel = $this->resolver->find($this->modelClass, $this->id);
63 5
        $this->model = $this->initialModel;
64 5
    }
65
66
    /**
67
     * @param ModelInterface|null $model
68
     * @return ModelReferenceInterface
69
     */
70 1 View Code Duplication
    public function setModel(ModelInterface $model = null): ModelReferenceInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 1
        $this->resolveModel();
73
74 1
        $this->model = $model;
75 1
        $this->id = null !== $model ? $model->getId() : null;
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * @return ModelInterface|null
82
     */
83 1
    public function getModel()
84
    {
85 1
        $this->resolveModel();
86
87 1
        return $this->model;
88
    }
89
90
    /**
91
     * @return null|string
92
     */
93 2
    public function getId()
94
    {
95 2
        return $this->id;
96
    }
97
98
    /**
99
     * @return ModelInterface|null
100
     */
101 1
    public function getInitialModel()
102
    {
103 1
        $this->resolveModel();
104
105 1
        return $this->initialModel;
106
    }
107
}
108