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\Account; |
15
|
|
|
use Xabbuh\XApi\Model\Actor as ActorModel; |
16
|
|
|
use Xabbuh\XApi\Model\Agent; |
17
|
|
|
use Xabbuh\XApi\Model\Group; |
18
|
|
|
use Xabbuh\XApi\Model\InverseFunctionalIdentifier; |
19
|
|
|
use Xabbuh\XApi\Model\IRI; |
20
|
|
|
use Xabbuh\XApi\Model\IRL; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Christian Flothmann <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Actor |
26
|
|
|
{ |
27
|
|
|
public $identifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
public $mbox; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
public $mboxSha1Sum; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
public $openId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
public $accountName; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string|null |
56
|
|
|
*/ |
57
|
|
|
public $accountHomePage; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|null |
61
|
|
|
*/ |
62
|
|
|
public $name; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Actor[]|null |
66
|
|
|
*/ |
67
|
|
|
public $members; |
68
|
|
|
|
69
|
|
|
public static function fromModel(ActorModel $model) |
70
|
|
|
{ |
71
|
|
|
$inverseFunctionalIdentifier = $model->getInverseFunctionalIdentifier(); |
72
|
|
|
|
73
|
|
|
$actor = new self(); |
74
|
|
|
$actor->mboxSha1Sum = $inverseFunctionalIdentifier->getMboxSha1Sum(); |
75
|
|
|
$actor->openId = $inverseFunctionalIdentifier->getOpenId(); |
76
|
|
|
|
77
|
|
|
if (null !== $mbox = $inverseFunctionalIdentifier->getMbox()) { |
78
|
|
|
$actor->mbox = $mbox->getValue(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
if (null !== $account = $inverseFunctionalIdentifier->getAccount()) { |
|
|
|
|
82
|
|
|
$actor->accountName = $account->getName(); |
83
|
|
|
$actor->accountHomePage = $account->getHomePage()->getValue(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($model instanceof Group) { |
87
|
|
|
$actor->type = 'group'; |
88
|
|
|
$actor->members = array(); |
89
|
|
|
|
90
|
|
|
foreach ($model->getMembers() as $agent) { |
91
|
|
|
$actor->members[] = Actor::fromModel($agent); |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
$actor->type = 'agent'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $actor; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function getModel() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$inverseFunctionalIdentifier = null; |
103
|
|
|
|
104
|
|
|
if (null !== $this->mbox) { |
105
|
|
|
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withMbox(IRI::fromString($this->mbox)); |
106
|
|
|
} elseif (null !== $this->mboxSha1Sum) { |
107
|
|
|
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withMboxSha1Sum($this->mboxSha1Sum); |
108
|
|
|
} elseif (null !== $this->openId) { |
109
|
|
|
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withOpenId($this->openId); |
110
|
|
|
} elseif (null !== $this->accountName && null !== $this->accountHomePage) { |
111
|
|
|
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withAccount(new Account($this->accountName, IRL::fromString($this->accountHomePage))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ('group' === $this->type) { |
115
|
|
|
$members = array(); |
116
|
|
|
|
117
|
|
|
foreach ($this->members as $agent) { |
|
|
|
|
118
|
|
|
$members[] = $agent->getModel(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return new Group($inverseFunctionalIdentifier, $this->name, $members); |
122
|
|
|
} else { |
123
|
|
|
return new Agent($inverseFunctionalIdentifier, $this->name); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.