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

Actor::getModel()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 26
Code Lines 17

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 15
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\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()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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()
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...
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) {
0 ignored issues
show
Bug introduced by
The expression $this->members of type array<integer,object<XAp...ne\Mapping\Actor>>|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...
118
                $members[] = $agent->getModel();
119
            }
120
121
            return new Group($inverseFunctionalIdentifier, $this->name, $members);
122
        } else {
123
            return new Agent($inverseFunctionalIdentifier, $this->name);
0 ignored issues
show
Bug introduced by
It seems like $inverseFunctionalIdentifier defined by null on line 102 can be null; however, Xabbuh\XApi\Model\Agent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
124
        }
125
    }
126
}
127