|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TiBian\BrowserRequirement; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Sinergi\BrowserDetector\Os; |
| 8 | +use Sinergi\BrowserDetector\Browser; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class BrowserRequirement |
| 12 | + * |
| 13 | + * @package TiBian\BrowserRequirement |
| 14 | + */ |
| 15 | +class BrowserRequirement |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Minimum Version of Supported Browsers |
| 19 | + * |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected $supportedVersions = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * Redirect Route on Unsupported Browser |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $routeUnsupportedBrowser; |
| 30 | + |
| 31 | + /** |
| 32 | + * Redirect Route on Supported Browser |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $routeSupportedBrowser; |
| 37 | + |
| 38 | + /** |
| 39 | + * Current Page |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected $currentPage; |
| 44 | + |
| 45 | + /** |
| 46 | + * Is Unsupported Browser |
| 47 | + * |
| 48 | + * @var bool |
| 49 | + */ |
| 50 | + protected $isUnsupportedBrowser; |
| 51 | + |
| 52 | + /** |
| 53 | + * OS Object |
| 54 | + * |
| 55 | + * @var object Os |
| 56 | + */ |
| 57 | + protected $os; |
| 58 | + |
| 59 | + /** |
| 60 | + * Browser Object |
| 61 | + * |
| 62 | + * @var object Browser |
| 63 | + */ |
| 64 | + protected $browser; |
| 65 | + |
| 66 | + /** |
| 67 | + * BrowserRequirement constructor. |
| 68 | + * |
| 69 | + * @param Browser $browser |
| 70 | + * @param Os $os |
| 71 | + */ |
| 72 | + public function __construct(Browser $browser, Os $os) |
| 73 | + { |
| 74 | + $this->supportedVersions = config('browser.requirement'); |
| 75 | + $this->routeUnsupportedBrowser = route(config('browser.routeUnsupportedBrowser')); |
| 76 | + $this->routeSupportedBrowser = route(config('browser.routeSupportedBrowser')); |
| 77 | + $this->currentPage = request()->url(); |
| 78 | + |
| 79 | + $this->os = $os; |
| 80 | + $this->browser = $browser; |
| 81 | + |
| 82 | + $this->isUnsupportedBrowser(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Determine if the Browser is Unsupported |
| 87 | + * |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + private function isUnsupportedBrowser() |
| 91 | + { |
| 92 | + if (array_key_exists($this->os->getName(), $this->supportedVersions)) { |
| 93 | + |
| 94 | + $browsers = $this->supportedVersions[$this->os->getName()]; |
| 95 | + |
| 96 | + foreach ($browsers as $browser => $version) { |
| 97 | + if ($this->browser->getName() === $browser && |
| 98 | + $this->browser->getVersion() < $version |
| 99 | + ) { |
| 100 | + return $this->isUnsupportedBrowser = true; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return $this->isUnsupportedBrowser = false; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Handle an incoming request. |
| 110 | + * |
| 111 | + * @param \Illuminate\Http\Request $request |
| 112 | + * @param \Closure $next |
| 113 | + * @return mixed |
| 114 | + */ |
| 115 | + public function handle(Request $request, Closure $next) |
| 116 | + { |
| 117 | + if ($this->isUnsupportedBrowser) { |
| 118 | + if ($this->currentPage != $this->routeUnsupportedBrowser) { |
| 119 | + return redirect($this->routeUnsupportedBrowser); |
| 120 | + } |
| 121 | + } else { |
| 122 | + if ($this->currentPage == $this->routeUnsupportedBrowser) { |
| 123 | + return redirect($this->routeSupportedBrowser); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $next($request); |
| 128 | + } |
| 129 | +} |
0 commit comments