diff options
author | 2022-07-01 15:10:30 +0200 | |
---|---|---|
committer | 2022-07-01 15:10:30 +0200 | |
commit | 4f75591060d95208a301bc6bf460d875631b29cc (patch) | |
tree | 4e37d86840e8d990a563ba75d3de6f84a53cc2de /lib/BridgeFactory.php | |
parent | 66568e3a39c61546c09a47a5688914a0bdf3c60c (diff) | |
download | rss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.tar.gz rss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.tar.zst rss-bridge-4f75591060d95208a301bc6bf460d875631b29cc.zip |
Reformat codebase v4 (#2872)
Reformat code base to PSR12
Co-authored-by: rssbridge <noreply@github.com>
Diffstat (limited to 'lib/BridgeFactory.php')
-rw-r--r-- | lib/BridgeFactory.php | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/lib/BridgeFactory.php b/lib/BridgeFactory.php index f435261c..3e355b7a 100644 --- a/lib/BridgeFactory.php +++ b/lib/BridgeFactory.php @@ -1,87 +1,87 @@ <?php -final class BridgeFactory { +final class BridgeFactory +{ + private $folder; + private $bridgeNames = []; + private $whitelist = []; - private $folder; - private $bridgeNames = []; - private $whitelist = []; + public function __construct(string $folder = PATH_LIB_BRIDGES) + { + $this->folder = $folder; - public function __construct(string $folder = PATH_LIB_BRIDGES) - { - $this->folder = $folder; + // create names + foreach (scandir($this->folder) as $file) { + if (preg_match('/^([^.]+)Bridge\.php$/U', $file, $m)) { + $this->bridgeNames[] = $m[1]; + } + } - // create names - foreach(scandir($this->folder) as $file) { - if(preg_match('/^([^.]+)Bridge\.php$/U', $file, $m)) { - $this->bridgeNames[] = $m[1]; - } - } + // create whitelist + if (file_exists(WHITELIST)) { + $contents = trim(file_get_contents(WHITELIST)); + } elseif (file_exists(WHITELIST_DEFAULT)) { + $contents = trim(file_get_contents(WHITELIST_DEFAULT)); + } else { + $contents = ''; + } + if ($contents === '*') { // Whitelist all bridges + $this->whitelist = $this->getBridgeNames(); + } else { + foreach (explode("\n", $contents) as $bridgeName) { + $this->whitelist[] = $this->sanitizeBridgeName($bridgeName); + } + } + } - // create whitelist - if (file_exists(WHITELIST)) { - $contents = trim(file_get_contents(WHITELIST)); - } elseif (file_exists(WHITELIST_DEFAULT)) { - $contents = trim(file_get_contents(WHITELIST_DEFAULT)); - } else { - $contents = ''; - } - if ($contents === '*') { // Whitelist all bridges - $this->whitelist = $this->getBridgeNames(); - } else { - foreach (explode("\n", $contents) as $bridgeName) { - $this->whitelist[] = $this->sanitizeBridgeName($bridgeName); - } - } - } + public function create(string $name): BridgeInterface + { + if (preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { + $className = sprintf('%sBridge', $this->sanitizeBridgeName($name)); + return new $className(); + } + throw new \InvalidArgumentException('Bridge name invalid!'); + } - public function create(string $name): BridgeInterface - { - if(preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { - $className = sprintf('%sBridge', $this->sanitizeBridgeName($name)); - return new $className(); - } - throw new \InvalidArgumentException('Bridge name invalid!'); - } + public function getBridgeNames(): array + { + return $this->bridgeNames; + } - public function getBridgeNames(): array - { - return $this->bridgeNames; - } + public function isWhitelisted($name): bool + { + return in_array($this->sanitizeBridgeName($name), $this->whitelist); + } - public function isWhitelisted($name): bool - { - return in_array($this->sanitizeBridgeName($name), $this->whitelist); - } + private function sanitizeBridgeName($name) + { + if (!is_string($name)) { + return null; + } - private function sanitizeBridgeName($name) { + // Trim trailing '.php' if exists + if (preg_match('/(.+)(?:\.php)/', $name, $matches)) { + $name = $matches[1]; + } - if(!is_string($name)) { - return null; - } + // Trim trailing 'Bridge' if exists + if (preg_match('/(.+)(?:Bridge)/i', $name, $matches)) { + $name = $matches[1]; + } - // Trim trailing '.php' if exists - if (preg_match('/(.+)(?:\.php)/', $name, $matches)) { - $name = $matches[1]; - } + // Improve performance for correctly written bridge names + if (in_array($name, $this->getBridgeNames())) { + $index = array_search($name, $this->getBridgeNames()); + return $this->getBridgeNames()[$index]; + } - // Trim trailing 'Bridge' if exists - if (preg_match('/(.+)(?:Bridge)/i', $name, $matches)) { - $name = $matches[1]; - } + // The name is valid if a corresponding bridge file is found on disk + if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeNames()))) { + $index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeNames())); + return $this->getBridgeNames()[$index]; + } - // Improve performance for correctly written bridge names - if (in_array($name, $this->getBridgeNames())) { - $index = array_search($name, $this->getBridgeNames()); - return $this->getBridgeNames()[$index]; - } - - // The name is valid if a corresponding bridge file is found on disk - if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeNames()))) { - $index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeNames())); - return $this->getBridgeNames()[$index]; - } - - Debug::log('Invalid bridge name specified: "' . $name . '"!'); - return null; - } + Debug::log('Invalid bridge name specified: "' . $name . '"!'); + return null; + } } |