Completed
Push — master ( 0ca4f0...992805 )
by Damien
07:27
created

AbstractProviderIdentityService::findByUserAndProviderOrCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 2/28/18
6
 * Time: 9:37 PM
7
 */
8
9
namespace flipbox\saml\core\services;
10
11
use craft\elements\User;
12
use flipbox\saml\core\EnsureSAMLPlugin;
13
use flipbox\saml\core\records\AbstractProvider;
14
use flipbox\saml\core\records\AbstractProviderIdentity;
15
use flipbox\saml\core\records\ProviderIdentityInterface;
16
use flipbox\saml\core\records\ProviderInterface;
17
use yii\base\Component;
18
use yii\helpers\Json;
19
20
/**
21
 * Class AbstractProviderIdentityService
22
 * @package flipbox\saml\core\services
23
 * @property \DateTime $lastLoginDate
24
 */
25
abstract class AbstractProviderIdentityService extends Component implements ProviderIdentityServiceInterface, EnsureSAMLPlugin
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function findByNameId(string $nameId, ProviderInterface $provider)
31
    {
32
        return $this->find([
33
            'nameId' => $nameId,
34
            'providerId' => $provider->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
35
        ]);
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function findByUser(User $user)
42
    {
43
        return $this->find([
44
            'userId' => $user->getId(),
45
        ]);
46
    }
47
48
    /**
49
     * @param User $user
50
     * @param AbstractProvider $provider
51
     * @return ProviderInterface|null
52
     */
53
    public function findByUserAndProvider(User $user, AbstractProvider $provider)
54
    {
55
        return $this->find([
56
            'userId' => $user->getId(),
57
            'providerId' => $provider->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<flipbox\saml\core...cords\AbstractProvider>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
58
        ])->one();
59
    }
60
61
    public function findByUserAndProviderOrCreate(User $user, AbstractProvider $provider)
62
    {
63
        $recordClass = $this->getPlugin()->getProviderIdentityRecordClass();
64
        return
65
            $this->findByUserAndProvider($user, $provider)
66
            ??
67
            new $recordClass([
68
                'nameId' => $provider->assignNameId($user),
69
                'providerId' => $provider->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<flipbox\saml\core...cords\AbstractProvider>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
70
                'enabled' => true,
71
                'userId' => $user->getId(),
72
                'lastLoginDate' => new \DateTime,
73
            ]);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function find($condition = [])
80
    {
81
        /** @var AbstractProviderIdentity $class */
82
        $class = $this->getPlugin()->getProviderIdentityRecordClass();
83
84
        /** @var AbstractProviderIdentity $class */
85
        $providerId = $class::find()->where($condition);
86
87
        return $providerId;
88
    }
89
90
    /**
91
     * @param ProviderIdentityInterface $record
92
     * @param bool $runValidation
93
     * @param array|null $attributeNames
94
     * @return ProviderIdentityInterface
95
     * @throws \Exception
96
     */
97
    public function save(
98
        ProviderIdentityInterface $record,
99
        $runValidation = true,
100
        $attributeNames = null
101
    ): ProviderIdentityInterface
102
    {
103
104
        if (! $record->save($runValidation, $attributeNames)) {
105
            throw new \Exception(Json::encode($record->getErrors()));
106
        }
107
108
        return $record;
109
    }
110
}
111