Completed
Push — master ( d63f8e...49a1eb )
by Christian
02:29
created

Statement::fromModel()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 32
nop 1
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
17
/**
18
 * A {@link Statement} mapped to a storage backend.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class Statement
23
{
24
    /**
25
     * @var string
26
     */
27
    public $id;
28
29
    /**
30
     * @var Object
31
     */
32
    public $actor;
33
34
    /**
35
     * @var Verb
36
     */
37
    public $verb;
38
39
    /**
40
     * @var Object
41
     */
42
    public $object;
43
44
    /**
45
     * @var Result
46
     */
47
    public $result;
48
49
    /**
50
     * @var Object
51
     */
52
    public $authority;
53
54
    /**
55
     * @var int
56
     */
57
    public $created;
58
59
    /**
60
     * @var int
61
     */
62
    public $stored;
63
64
    /**
65
     * @var Context
66
     */
67
    public $context;
68
69
    /**
70
     * @var bool
71
     */
72
    public $hasAttachments;
73
74
    /**
75
     * @var Attachment[]|null
76
     */
77
    public $attachments;
78
79
    public static function fromModel(StatementModel $model)
80
    {
81
        $statement = new self();
82
        $statement->id = $model->getId()->getValue();
83
        $statement->actor = Object::fromModel($model->getActor());
84
        $statement->verb = Verb::fromModel($model->getVerb());
85
        $statement->object = Object::fromModel($model->getObject());
86
87
        if (null !== $model->getTimestamp()) {
88
            $statement->created = $model->getTimestamp()->getTimestamp();
89
        }
90
91
        if (null !== $result = $model->getResult()) {
92
            $statement->result = Result::fromModel($result);
93
        }
94
95
        if (null !== $authority = $model->getAuthority()) {
96
            $statement->authority = Object::fromModel($authority);
97
        }
98
99
        if (null !== $context = $model->getContext()) {
100
            $statement->context = Context::fromModel($context);
101
        }
102
103
        if (null !== $attachments = $model->getAttachments()) {
104
            $statement->hasAttachments = true;
105
            $statement->attachments = array();
106
107
            foreach ($attachments as $attachment) {
108
                $mappedAttachment = Attachment::fromModel($attachment);
109
                $mappedAttachment->statement = $statement;
110
                $statement->attachments[] = $mappedAttachment;
111
            }
112
        } else {
113
            $statement->hasAttachments = false;
114
        }
115
116
        return $statement;
117
    }
118
119
    public function getModel()
120
    {
121
        $result = null;
122
        $authority = null;
123
        $created = null;
124
        $stored = null;
125
        $context = null;
126
        $attachments = null;
127
128
        if (null !== $this->result) {
129
            $result = $this->result->getModel();
130
        }
131
132
        if (null !== $this->authority) {
133
            $authority = $this->authority->getModel();
134
        }
135
136
        if (null !== $this->created) {
137
            $created = new \DateTime('@'.$this->created);
138
        }
139
140
        if (null !== $this->stored) {
141
            $stored = new \DateTime('@'.$this->stored);
142
        }
143
144
        if (null !== $this->context) {
145
            $context = $this->context->getModel();
146
        }
147
148
        if ($this->hasAttachments) {
149
            $attachments = array();
150
151
            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...
152
                $attachments[] = $attachment->getModel();
153
            }
154
        }
155
156
        return new StatementModel(
157
            StatementId::fromString($this->id),
158
            $this->actor->getModel(),
159
            $this->verb->getModel(),
160
            $this->object->getModel(),
161
            $result,
162
            $authority,
163
            $created,
164
            $stored,
165
            $context,
166
            $attachments
0 ignored issues
show
Bug introduced by
It seems like $attachments defined by array() on line 149 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...
167
        );
168
    }
169
}
170