| Conditions | 15 |
| Paths | 5390 |
| Total Lines | 120 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 13 | function metarefresh_hook_cron(array &$croninfo): void |
||
| 14 | { |
||
| 15 | Assert::keyExists($croninfo, 'summary'); |
||
| 16 | Assert::keyExists($croninfo, 'tag'); |
||
| 17 | |||
| 18 | Logger::info('cron [metarefresh]: Running cron in cron tag [' . $croninfo['tag'] . '] '); |
||
| 19 | |||
| 20 | try { |
||
| 21 | $config = Configuration::getInstance(); |
||
| 22 | $mconfig = Configuration::getOptionalConfig('config-metarefresh.php'); |
||
| 23 | |||
| 24 | $sets = $mconfig->getArray('sets', []); |
||
| 25 | /** @var string $datadir */ |
||
| 26 | $datadir = $config->getPathValue('datadir', 'data/'); |
||
| 27 | $stateFile = $datadir . 'metarefresh-state.php'; |
||
| 28 | |||
| 29 | foreach ($sets as $setkey => $set) { |
||
| 30 | $set = Configuration::loadFromArray($set); |
||
| 31 | |||
| 32 | // Only process sets where cron matches the current cron tag |
||
| 33 | $cronTags = $set->getArray('cron'); |
||
| 34 | if (!in_array($croninfo['tag'], $cronTags, true)) { |
||
| 35 | continue; |
||
| 36 | } |
||
| 37 | |||
| 38 | Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']'); |
||
| 39 | |||
| 40 | $expireAfter = $set->getInteger('expireAfter', null); |
||
| 41 | if ($expireAfter !== null) { |
||
| 42 | $expire = time() + $expireAfter; |
||
| 43 | } else { |
||
| 44 | $expire = null; |
||
| 45 | } |
||
| 46 | |||
| 47 | $outputDir = $set->getString('outputDir'); |
||
| 48 | $outputDir = $config->resolvePath($outputDir); |
||
| 49 | if ($outputDir === null) { |
||
| 50 | throw new \Exception("Invalid outputDir specified."); |
||
| 51 | } |
||
| 52 | |||
| 53 | $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile'); |
||
| 54 | |||
| 55 | $oldMetadataSrc = \SimpleSAML\Metadata\MetaDataStorageSource::getSource([ |
||
| 56 | 'type' => $outputFormat, |
||
| 57 | 'directory' => $outputDir, |
||
| 58 | ]); |
||
| 59 | |||
| 60 | $metaloader = new \SimpleSAML\Module\metarefresh\MetaLoader($expire, $stateFile, $oldMetadataSrc); |
||
| 61 | |||
| 62 | // Get global blacklist, whitelist, attributewhitelist and caching info |
||
| 63 | $blacklist = $mconfig->getArray('blacklist', []); |
||
| 64 | $whitelist = $mconfig->getArray('whitelist', []); |
||
| 65 | $attributewhitelist = $mconfig->getArray('attributewhitelist', []); |
||
| 66 | $conditionalGET = $mconfig->getBoolean('conditionalGET', false); |
||
| 67 | |||
| 68 | // get global type filters |
||
| 69 | $available_types = [ |
||
| 70 | 'saml20-idp-remote', |
||
| 71 | 'saml20-sp-remote', |
||
| 72 | 'attributeauthority-remote' |
||
| 73 | ]; |
||
| 74 | $set_types = $set->getArrayize('types', $available_types); |
||
| 75 | |||
| 76 | foreach ($set->getArray('sources') as $source) { |
||
| 77 | // filter metadata by type of entity |
||
| 78 | if (isset($source['types'])) { |
||
| 79 | $metaloader->setTypes($source['types']); |
||
| 80 | } else { |
||
| 81 | $metaloader->setTypes($set_types); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Merge global and src specific blacklists |
||
| 85 | if (isset($source['blacklist'])) { |
||
| 86 | $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist)); |
||
| 87 | } else { |
||
| 88 | $source['blacklist'] = $blacklist; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Merge global and src specific whitelists |
||
| 92 | if (isset($source['whitelist'])) { |
||
| 93 | $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist)); |
||
| 94 | } else { |
||
| 95 | $source['whitelist'] = $whitelist; |
||
| 96 | } |
||
| 97 | |||
| 98 | # Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim. |
||
| 99 | if (isset($source['attributewhitelist'])) { |
||
| 100 | $source['attributewhitelist'] = array_merge($source['attributewhitelist'], $attributewhitelist); |
||
| 101 | } else { |
||
| 102 | $source['attributewhitelist'] = $attributewhitelist; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Let src specific conditionalGET override global one |
||
| 106 | if (!isset($source['conditionalGET'])) { |
||
| 107 | $source['conditionalGET'] = $conditionalGET; |
||
| 108 | } |
||
| 109 | |||
| 110 | Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']'); |
||
| 111 | $metaloader->loadSource($source); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Write state information back to disk |
||
| 115 | $metaloader->writeState(); |
||
| 116 | |||
| 117 | switch ($outputFormat) { |
||
| 118 | case 'flatfile': |
||
| 119 | $metaloader->writeMetadataFiles($outputDir); |
||
| 120 | break; |
||
| 121 | case 'serialize': |
||
| 122 | $metaloader->writeMetadataSerialize($outputDir); |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($set->hasValue('arp')) { |
||
| 127 | $arpconfig = Configuration::loadFromArray($set->getValue('arp')); |
||
|
|
|||
| 128 | $metaloader->writeARPfile($arpconfig); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | $croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage(); |
||
| 133 | } |
||
| 135 |