Completed
Push — master ( 89c2c1...306671 )
by Damien
07:22
created

MetadataController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 53
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A actionRefreshWithUrl() 0 43 3
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
11
/**
12
 * Automate Provider Operations
13
 * Class MetadataController
14
 * @package flipbox\saml\sp\commands
15
 */
16
class MetadataController extends Controller
17
{
18
19
    /**
20
     * Refresh and save the metadata from the URL saved on the provider.
21
     * Configure the URL in the Craft Control Panel before using this.
22
     *
23
     * @param $uid
24
     */
25
    public function actionRefreshWithUrl(string $uid)
26
    {
27
        $message =
28
            sprintf('Trying Provider with uid %s.', $uid);
29
        $this->stderr($message . PHP_EOL);
30
        Saml::info($message);
31
32
        /** @var ProviderRecord $provider */
33
        if (! $provider = Saml::getInstance()->getProvider()->findByIdp([
34
            'uid' => $uid,
35
        ])->one()) {
36
            $message =
37
                sprintf('Provider with uid %s not found.', $uid);
38
            $this->stderr(
39
                $message
40
            );
41
            Saml::warning($message);
42
            return ExitCode::DATAERR;
43
        }
44
45
        if (! $url = $provider->getMetadataOptions()->url) {
46
            $message =
47
                sprintf('Provider with uid %s does not have a metadata url.', $uid);
48
            $this->stderr($message . PHP_EOL);
49
            Saml::warning($message);
50
            return ExitCode::DATAERR;
51
        }
52
        $message =
53
            sprintf('Provider %s (%s) found with URL %s.', $uid, $provider->getEntityId(), $url);
54
        $this->stderr($message . PHP_EOL);
55
        Saml::info($message);
56
57
        $entityDescriptor = Saml::getInstance()->getMetadata()->fetchByUrl($url);
58
59
        $provider->metadata = $entityDescriptor->toXML()->ownerDocument->saveXML();
60
61
        Saml::getInstance()->getProvider()->save($provider);
62
        $message = sprintf('Provider %s saved! (%s)', $uid, $entityDescriptor->getEntityID());
63
        Saml::info($message);
64
        $this->stdout($message . PHP_EOL);
65
66
        return ExitCode::OK;
67
    }
68
}
69