Completed
Push — develop ( b386e5...33ac12 )
by Damien
12:18 queued 14s
created

AbstractProviderService::providerMappingToKeyValue()   A

Complexity

Conditions 3
Paths 3

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 3
nc 3
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 2/10/18
6
 * Time: 12:12 AM
7
 */
8
9
namespace flipbox\saml\core\services;
10
11
use craft\base\Component;
12
use craft\helpers\Json;
13
use flipbox\keychain\records\KeyChainRecord;
14
use flipbox\saml\core\helpers\SerializeHelper;
15
use flipbox\saml\core\records\AbstractProvider;
16
use flipbox\saml\core\records\LinkRecord;
17
use flipbox\saml\core\records\ProviderInterface;
18
use flipbox\saml\core\traits\EnsureSamlPlugin;
19
use LightSaml\Model\Metadata\EntityDescriptor;
20
21
abstract class AbstractProviderService extends Component implements ProviderServiceInterface
22
{
23
24
    use EnsureSamlPlugin;
25
26
    /**
27
     * @inheritdoc
28
     */
29
    abstract public function findOwn();
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function find($condition = [])
35
    {
36
        /** @var AbstractProvider $class */
37
        $class = $this->getSamlPlugin()->getProviderRecordClass();
38
39
        if (! $provider = $class::find()->where($condition)) {
40
            return null;
41
        }
42
43
        return $provider;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function findByIdp($condition = [])
50
    {
51
        return $this->findByType('idp', $condition);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function findBySp($condition = [])
58
    {
59
        return $this->findByType('sp', $condition);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    protected function findByType($type, $condition = [])
66
    {
67
        if (! in_array($type, ['sp', 'idp'])) {
68
            throw new \InvalidArgumentException("Type must be idp or sp.");
69
        }
70
        return $this->find(
71
            array_merge(
72
                [
73
                    'enabled'      => 1,
74
                    'providerType' => $type,
75
                ],
76
                $condition
77
            )
78
        );
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function findByEntityId($entityId)
85
    {
86
        return $this->find([
87
            'entityId' => $entityId,
88
        ]);
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function create(EntityDescriptor $entityDescriptor, KeyChainRecord $keyChainRecord = null): ProviderInterface
95
    {
96
97
        $recordClass = $this->getSamlPlugin()->getProviderRecordClass();
98
99
        /** @var ProviderInterface $provider */
100
        $provider = (new $recordClass())
101
            ->loadDefaultValues();
102
103
104
        $provider->providerType = $this->getSamlPlugin()->getMyType();
0 ignored issues
show
Bug introduced by
Accessing providerType 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...
105
106
        \Craft::configure($provider, [
107
            'entityId' => $entityDescriptor->getEntityID(),
108
            'metadata' => SerializeHelper::toXml($entityDescriptor),
109
        ]);
110
111
        if ($keyChainRecord) {
112
            $provider->setKeychain($keyChainRecord);
113
        }
114
115
        return $provider;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function save(AbstractProvider $record, $runValidation = true, $attributeNames = null)
122
    {
123
        if ($record->isNewRecord) {
124
            $record->loadDefaultValues();
125
        }
126
127
        //save record
128
        if (! $record->save($runValidation, $attributeNames)) {
129
            throw new \Exception(Json::encode($record->getErrors()));
130
        }
131
132
        if ($record->keychain) {
0 ignored issues
show
Bug introduced by
The property keychain does not seem to exist. Did you mean cachedKeychain?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
            $this->linkToKey(
134
                $record,
135
                $record->keychain
0 ignored issues
show
Bug introduced by
The property keychain does not seem to exist. Did you mean cachedKeychain?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
            );
137
        }
138
139
        return $record;
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145
    public function linkToKey(
146
        AbstractProvider $provider,
147
        KeyChainRecord $keyChain,
148
        $runValidation = true,
149
        $attributeNames = null
150
    ) {
151
        if (! $provider->id && ! $keyChain->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...
152
            throw new \Exception('Provider id and keychain id must exist before linking.');
153
        }
154
        $linkAttributes = [
155
            '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...
156
        ];
157
158
        /** @var LinkRecord $link */
159
        if (! $link = LinkRecord::find()->where($linkAttributes)->one()) {
160
            $link = new LinkRecord($linkAttributes);
161
        }
162
163
        $linkAttributes['keyChainId'] = $keyChain->id;
164
        \Craft::configure(
165
            $link,
0 ignored issues
show
Bug introduced by
It seems like $link defined by \flipbox\saml\core\recor...$linkAttributes)->one() on line 159 can also be of type array; however, yii\BaseYii::configure() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
166
            $linkAttributes
167
        );
168
        if (! $link->save($runValidation, $attributeNames)) {
169
            throw new \Exception(Json::encode($record->getErrors()));
170
        }
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function delete(ProviderInterface $provider)
177
    {
178
        return $provider->delete();
179
    }
180
}
181