Completed
Pull Request — master (#3)
by Christian
02:33
created

Statement::getModel()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 50
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 6.3636
c 0
b 0
f 0
cc 8
eloc 32
nc 64
nop 0
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace XApi\Repository\Doctrine\Mapping;
13
14
use Xabbuh\XApi\Model\Statement as StatementModel;
15
use Xabbuh\XApi\Model\StatementId;
16
use Xabbuh\XApi\Model\StatementReference;
17
18
/**
19
 * A {@link Statement} mapped to a storage backend.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
class Statement
24
{
25
    /**
26
     * @var string
27
     */
28
    public $id;
29
30
    /**
31
     * @var Object
32
     */
33
    public $actor;
34
35
    /**
36
     * @var Verb
37
     */
38
    public $verb;
39
40
    /**
41
     * @var Object
42
     */
43
    public $object;
44
45
    /**
46
     * @var Result
47
     */
48
    public $result;
49
50
    /**
51
     * @var Object
52
     */
53
    public $authority;
54
55
    /**
56
     * @var int
57
     */
58
    public $created;
59
60
    /**
61
     * @var int
62
     */
63
    public $stored;
64
65
    /**
66
     * @var Context
67
     */
68
    public $context;
69
70
    /**
71
     * @var bool
72
     */
73
    public $hasAttachments;
74
75
    /**
76
     * @var Attachment[]|null
77
     */
78
    public $attachments;
79
80
    public static function fromModel(StatementModel $model)
81
    {
82
        $statement = new self();
83
        $statement->id = $model->getId()->getValue();
84
        $statement->actor = Object::fromModel($model->getActor());
85
        $statement->verb = Verb::fromModel($model->getVerb());
86
        $statement->object = Object::fromModel($model->getObject());
87
88
        if (null !== $model->getTimestamp()) {
89
            $statement->created = $model->getTimestamp()->getTimestamp();
90
        }
91
92
        if (null !== $result = $model->getResult()) {
93
            $statement->result = Result::fromModel($result);
94
        }
95
96
        if (null !== $authority = $model->getAuthority()) {
97
            $statement->authority = Object::fromModel($authority);
98
        }
99
100
        if (null !== $context = $model->getContext()) {
101
            $statement->context = Context::fromModel($context);
102
        }
103
104
        if (null !== $attachments = $model->getAttachments()) {
105
            $statement->hasAttachments = true;
106
            $statement->attachments = array();
107
108
            foreach ($attachments as $attachment) {
109
                $mappedAttachment = Attachment::fromModel($attachment);
110
                $mappedAttachment->statement = $statement;
111
                $statement->attachments[] = $mappedAttachment;
112
            }
113
        } else {
114
            $statement->hasAttachments = false;
115
        }
116
117
        return $statement;
118
    }
119
120
    public function getModel()
121
    {
122
        $result = null;
123
        $authority = null;
124
        $created = null;
125
        $stored = null;
126
        $context = null;
127
        $attachments = null;
128
129
        if (null !== $this->result) {
130
            $result = $this->result->getModel();
131
        }
132
133
        if (null !== $this->authority) {
134
            $authority = $this->authority->getModel();
135
        }
136
137
        if (null !== $this->created) {
138
            $created = new \DateTime('@'.$this->created);
139
        }
140
141
        if (null !== $this->stored) {
142
            $stored = new \DateTime('@'.$this->stored);
143
        }
144
145
        if (null !== $this->context) {
146
            $context = $this->context->getModel();
147
        }
148
149
        if ($this->hasAttachments) {
150
            $attachments = array();
151
152
            foreach ($this->attachments as $attachment) {
0 ignored issues
show
Bug introduced by
The expression $this->attachments of type array<integer,object<XAp...pping\Attachment>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
153
                $attachments[] = $attachment->getModel();
154
            }
155
        }
156
157
        return new StatementModel(
158
            StatementId::fromString($this->id),
159
            $this->actor->getModel(),
160
            $this->verb->getModel(),
161
            $this->object->getModel(),
162
            $result,
163
            $authority,
164
            $created,
165
            $stored,
166
            $context,
167
            $attachments
0 ignored issues
show
Bug introduced by
It seems like $attachments defined by array() on line 150 can also be of type array; however, Xabbuh\XApi\Model\Statement::__construct() does only seem to accept null|array<integer,objec...XApi\Model\Attachment>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
168
        );
169
    }
170
}
171