Login   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 73
c 0
b 0
f 0
ccs 0
cts 27
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A byIdentity() 0 17 3
A transformToUser() 0 42 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 1/11/18
6
 * Time: 8:30 PM
7
 */
8
9
namespace flipbox\saml\sp\services;
10
11
use craft\base\Component;
12
use craft\elements\User;
13
use flipbox\saml\core\exceptions\InvalidMessage;
14
use flipbox\saml\core\helpers\MessageHelper;
15
use flipbox\saml\sp\events\UserLogin;
16
use flipbox\saml\sp\models\Settings;
17
use flipbox\saml\sp\records\ProviderIdentityRecord;
18
use flipbox\saml\sp\records\ProviderRecord;
19
use flipbox\saml\sp\Saml;
20
use flipbox\saml\sp\services\login\AssertionTrait;
21
use SAML2\Response as SamlResponse;
22
use yii\base\UserException;
23
24
/**
25
 * Class Consumer
26
 * @package flipbox\saml\sp\services\login
27
 */
28
class Login extends Component
29
{
30
    use AssertionTrait;
31
32
    const EVENT_BEFORE_RESPONSE_TO_USER = 'eventBeforeResponseToUser';
33
    const EVENT_AFTER_RESPONSE_TO_USER = 'eventAfterResponseToUser';
34
35
    public function transformToUser(
36
        User $user,
37
        SamlResponse $response,
38
        ProviderRecord $idp,
39
        ProviderRecord $sp,
40
        Settings $settings
41
    ) {
42
        /**
43
         * Before user transformation
44
         */
45
        $event = new UserLogin();
46
        $event->response = $response;
47
        $event->user = $user;
48
49
        $this->trigger(
50
            static::EVENT_BEFORE_RESPONSE_TO_USER,
51
            $event
52
        );
53
54
        // Sync
55
        Saml::getInstance()->getUser()->sync(
56
            $user,
57
            $response,
58
            $idp,
59
            $sp,
60
            $settings
61
        );
62
63
        /**
64
         * After user save
65
         */
66
        $event = new UserLogin();
67
        $event->response = $response;
68
        $event->user = $user;
69
70
        $this->trigger(
71
            static::EVENT_AFTER_RESPONSE_TO_USER,
72
            $event
73
        );
74
75
        return $user;
76
    }
77
78
    /**
79
     * @param ProviderIdentityRecord $identityRecord
80
     * @throws UserException
81
     * @throws \Throwable
82
     */
83
    public function byIdentity(ProviderIdentityRecord $identityRecord)
84
    {
85
        /**
86
         * Log user in
87
         */
88
        if (! Saml::getInstance()->getUser()->login($identityRecord)) {
89
            throw new UserException("Unknown error while logging in.");
90
        }
91
        /**
92
         * User's successfully logged in so we can now set the lastLogin for the
93
         * provider identity and save it to the db.
94
         */
95
        $identityRecord->lastLoginDate = new \DateTime();
0 ignored issues
show
Documentation introduced by
The property lastLoginDate does not exist on object<flipbox\saml\sp\r...ProviderIdentityRecord>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
        if (! Saml::getInstance()->getProviderIdentity()->save($identityRecord)) {
97
            throw new UserException("Error while saving identity.");
98
        }
99
    }
100
}
101