Completed
Push — master ( aee7cd...a8fd05 )
by Damien
07:18
created

MetadataController::actionIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 27
cp 0
rs 9.44
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
4
namespace flipbox\saml\sp\commands;
5
6
use craft\console\Controller;
7
use flipbox\saml\sp\records\ProviderRecord;
8
use flipbox\saml\sp\Saml;
9
use yii\console\ExitCode;
10
use yii\console\widgets\Table;
11
12
/**
13
 * Automate Provider Operations
14
 * Class MetadataController
15
 * @package flipbox\saml\sp\commands
16
 */
17
class MetadataController extends Controller
18
{
19
20
    public function actionIndex()
21
    {
22
        $providerQuery = ProviderRecord::find();
23
24
        $rows = [];
25
        foreach ($providerQuery->all() as $provider) {
26
            /** @var ProviderRecord $provider */
27
28
            $rows[] = [
29
                $provider->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<flipbox\saml\sp\records\ProviderRecord>. 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...
30
                $provider->getType(),
31
                $provider->label,
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<flipbox\saml\sp\records\ProviderRecord>. 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...
32
                $provider->entityId,
0 ignored issues
show
Documentation introduced by
The property entityId does not exist on object<flipbox\saml\sp\records\ProviderRecord>. 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...
33
                $provider->uid,
34
                $provider->enabled ? 'enabled' : 'disabled',
35
            ];
36
        }
37
        $this->stdout(Table::widget([
38
            'headers' => [
39
                'ID',
40
                'TYPE',
41
                'LABEL',
42
                'ENTITY ID',
43
                'UID',
44
                'ENABLED/DISABLED',
45
            ],
46
            'rows' => $rows,
47
        ]));
48
        return ExitCode::OK;
49
    }
50
51
    /**
52
     * Refresh and save the metadata from the URL saved on the provider.
53
     * Configure the URL in the Craft Control Panel before using this.
54
     *
55
     * @param $uid
56
     */
57
    public function actionRefreshWithUrl(string $uid)
58
    {
59
        $message =
60
            sprintf('Trying Provider with uid %s.', $uid);
61
        $this->stderr($message . PHP_EOL);
62
        Saml::info($message);
63
64
        /** @var ProviderRecord $provider */
65
        if (! $provider = Saml::getInstance()->getProvider()->findByIdp([
66
            'uid' => $uid,
67
        ])->one()) {
68
            $message =
69
                sprintf('Provider with uid %s not found.', $uid);
70
            $this->stderr(
71
                $message
72
            );
73
            Saml::warning($message);
74
            return ExitCode::DATAERR;
75
        }
76
77
        if (! $url = $provider->getMetadataOptions()->url) {
78
            $message =
79
                sprintf('Provider with uid %s does not have a metadata url.', $uid);
80
            $this->stderr($message . PHP_EOL);
81
            Saml::warning($message);
82
            return ExitCode::DATAERR;
83
        }
84
        $message =
85
            sprintf('Provider %s (%s) found with URL %s.', $uid, $provider->getEntityId(), $url);
86
        $this->stderr($message . PHP_EOL);
87
        Saml::info($message);
88
89
        $entityDescriptor = Saml::getInstance()->getMetadata()->fetchByUrl($url);
90
91
        $provider->metadata = $entityDescriptor->toXML()->ownerDocument->saveXML();
92
        $provider->setMetadataModel($entityDescriptor);
93
94
        Saml::getInstance()->getProvider()->save($provider);
95
        $message = sprintf('Provider %s saved! (%s)', $uid, $entityDescriptor->getEntityID());
96
        Saml::info($message);
97
        $this->stdout($message . PHP_EOL);
98
99
        return ExitCode::OK;
100
    }
101
}
102