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

Context::fromModel()   D

Complexity

Conditions 14
Paths 272

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 4.1153
c 0
b 0
f 0
cc 14
eloc 43
nc 272
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Context as ContextModel;
15
use Xabbuh\XApi\Model\ContextActivities;
16
use Xabbuh\XApi\Model\StatementId;
17
use Xabbuh\XApi\Model\StatementReference;
18
19
/**
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class Context
23
{
24
    public $identifier;
25
26
    /**
27
     * @var string|null
28
     */
29
    public $registration;
30
31
    /**
32
     * @var Object|null
33
     */
34
    public $instructor;
35
36
    /**
37
     * @var Object|null
38
     */
39
    public $team;
40
41
    /**
42
     * @var bool|null
43
     */
44
    public $hasContextActivities;
45
46
    /**
47
     * @var Object[]|null
48
     */
49
    public $parentActivities;
50
51
    /**
52
     * @var Object[]|null
53
     */
54
    public $groupingActivities;
55
56
    /**
57
     * @var Object[]|null
58
     */
59
    public $categoryActivities;
60
61
    /**
62
     * @var Object[]|null
63
     */
64
    public $otherActivities;
65
66
    /**
67
     * @var string|null
68
     */
69
    public $revision;
70
71
    /**
72
     * @var string|null
73
     */
74
    public $platform;
75
76
    /**
77
     * @var string|null
78
     */
79
    public $language;
80
81
    /**
82
     * @var string|null
83
     */
84
    public $statement;
85
86
    /**
87
     * @var Extensions|null
88
     */
89
    public $extensions;
90
91
    public static function fromModel(ContextModel $model)
92
    {
93
        $context = new self();
94
        $context->registration = $model->getRegistration();
95
        $context->revision = $model->getRevision();
96
        $context->platform = $model->getPlatform();
97
        $context->language = $model->getLanguage();
98
99
        if (null !== $instructor = $model->getInstructor()) {
100
            $context->instructor = Object::fromModel($instructor);
101
        }
102
103
        if (null !== $team = $model->getTeam()) {
104
            $context->team = Object::fromModel($team);
105
        }
106
107
        if (null !== $contextActivities = $model->getContextActivities()) {
108
            $context->hasContextActivities = true;
109
110
            if (null !== $parentActivities = $contextActivities->getParentActivities()) {
111
                $context->parentActivities = array();
112
113
                foreach ($parentActivities as $parentActivity) {
114
                    $activity = Object::fromModel($parentActivity);
115
                    $activity->parentContext = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context of type object<XApi\Repository\Doctrine\Mapping\Context> is incompatible with the declared type object<XApi\Repository\D...Mapping\Statement>|null of property $parentContext.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
                    $context->parentActivities[] = $activity;
117
                }
118
            }
119
120
            if (null !== $groupingActivities = $contextActivities->getGroupingActivities()) {
121
                $context->groupingActivities = array();
122
123
                foreach ($groupingActivities as $groupingActivity) {
124
                    $activity = Object::fromModel($groupingActivity);
125
                    $activity->groupingContext = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context of type object<XApi\Repository\Doctrine\Mapping\Context> is incompatible with the declared type object<XApi\Repository\D...Mapping\Statement>|null of property $groupingContext.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
126
                    $context->groupingActivities[] = $activity;
127
                }
128
            }
129
130
            if (null !== $categoryActivities = $contextActivities->getCategoryActivities()) {
131
                $context->categoryActivities = array();
132
133
                foreach ($categoryActivities as $categoryActivity) {
134
                    $activity = Object::fromModel($categoryActivity);
135
                    $activity->categoryContext = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context of type object<XApi\Repository\Doctrine\Mapping\Context> is incompatible with the declared type object<XApi\Repository\D...Mapping\Statement>|null of property $categoryContext.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
136
                    $context->categoryActivities[] = $activity;
137
                }
138
            }
139
140
            if (null !== $otherActivities = $contextActivities->getOtherActivities()) {
141
                $context->otherActivities = array();
142
143
                foreach ($otherActivities as $otherActivity) {
144
                    $activity = Object::fromModel($otherActivity);
145
                    $activity->otherContext = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context of type object<XApi\Repository\Doctrine\Mapping\Context> is incompatible with the declared type object<XApi\Repository\D...Mapping\Statement>|null of property $otherContext.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
146
                    $context->otherActivities[] = $activity;
147
                }
148
            }
149
        } else {
150
            $context->hasContextActivities = false;
151
        }
152
153
        if (null !== $statementReference = $model->getStatement()) {
154
            $context->statement = $statementReference->getStatementId()->getValue();
155
        }
156
157
        if (null !== $contextExtensions = $model->getExtensions()) {
158
            $context->extensions = Extensions::fromModel($contextExtensions);
159
        }
160
161
        return $context;
162
    }
163
164
    public function getModel()
165
    {
166
        $context = new ContextModel();
167
        $context = $context->withRegistration($this->registration);
168
        $context = $context->withRevision($this->revision);
169
        $context = $context->withPlatform($this->platform);
170
        $context = $context->withLanguage($this->language);
171
172
        if (null !== $this->instructor) {
173
            $context = $context->withInstructor($this->instructor->getModel());
174
        }
175
176
        if (null !== $this->team) {
177
            $context = $context->withTeam($this->team->getModel());
178
        }
179
180
        if ($this->hasContextActivities) {
181
            $contextActivities = new ContextActivities();
182
183
            if (null !== $this->parentActivities) {
184
                foreach ($this->parentActivities as $contextParentActivity) {
185
                    $contextActivities = $contextActivities->withAddedParentActivity($contextParentActivity->getModel());
186
                }
187
            }
188
189
            if (null !== $this->groupingActivities) {
190
                foreach ($this->groupingActivities as $contextGroupingActivity) {
191
                    $contextActivities = $contextActivities->withAddedGroupingActivity($contextGroupingActivity->getModel());
192
                }
193
            }
194
195
            if (null !== $this->categoryActivities) {
196
                foreach ($this->categoryActivities as $contextCategoryActivity) {
197
                    $contextActivities = $contextActivities->withAddedCategoryActivity($contextCategoryActivity->getModel());
198
                }
199
            }
200
201
            if (null !== $this->otherActivities) {
202
                foreach ($this->otherActivities as $contextOtherActivity) {
203
                    $contextActivities = $contextActivities->withAddedOtherActivity($contextOtherActivity->getModel());
204
                }
205
            }
206
207
            $context = $context->withContextActivities($contextActivities);
208
        }
209
210
        if (null !== $this->statement) {
211
            $context = $context->withStatement(new StatementReference(StatementId::fromString($this->statement)));
212
        }
213
214
        if (null !== $this->extensions) {
215
            $context = $context->withExtensions($this->extensions->getModel());
216
        }
217
218
        return $context;
219
    }
220
}
221