Passed
Push — master ( 74b41a...e78beb )
by Dominik
07:41
created

LazyModelReference::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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 7
    public function __construct(ResolverInterface $resolver, string $modelClass, string $id = null)
48
    {
49 7
        $this->resolver = $resolver;
50 7
        $this->modelClass = $modelClass;
51 7
        $this->id = $id;
52 7
    }
53
54 7
    private function resolveModel()
55
    {
56 7
        if ($this->resolved) {
57 4
            return;
58
        }
59
60 7
        $this->resolved = true;
61
62 7
        $this->initialModel = $this->resolver->find($this->modelClass, $this->id);
63 7
        $this->model = $this->initialModel;
64 7
    }
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
    /**
109
     * @return array|null
110
     */
111 2
    public function jsonSerialize()
112
    {
113 2
        $this->resolveModel();
114
115 2
        if (null === $this->model) {
116 1
            return null;
117
        }
118
119 1
        $this->jsonSerializableOrException();
120
121 1
        return $this->model->jsonSerialize();
0 ignored issues
show
Bug introduced by
The method jsonSerialize() does not seem to exist on object<Chubbyphp\Model\ModelInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
    }
123
124
    /**
125
     * @throws \LogicException
126
     */
127 View Code Duplication
    private function jsonSerializableOrException()
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...
128
    {
129
        if (!$this->model instanceof \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
130
            throw new \LogicException(
131
                sprintf('Model %s does not implement %s', get_class($this->model), \JsonSerializable::class)
132
            );
133
        }
134
    }
135
}
136