-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jprodrigues70/master
It increases code readability, makes it easier to use and adds some new features
- Loading branch information
Showing
14 changed files
with
388 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Potelo\LaravelBlockBots\Abstracts; | ||
|
||
use Potelo\LaravelBlockBots\Contracts\Configuration; | ||
use Potelo\LaravelBlockBots\Contracts\Client; | ||
use Carbon\Carbon; | ||
|
||
abstract class AbstractBlockBots | ||
{ | ||
private $allowedBots = []; | ||
protected $request; | ||
protected $limit; | ||
protected $frequency; | ||
protected $options; | ||
protected $client; | ||
protected $timeOutAt; | ||
protected $hits = 1; | ||
|
||
public function __construct() | ||
{ | ||
$this->options = new Configuration(); | ||
} | ||
|
||
public function setUp($request, $limit, $frequency) | ||
{ | ||
$this->setRequest($request); | ||
$this->setLimit($limit); | ||
$this->setFrequency($frequency); | ||
$this->setTimeOut($frequency); | ||
$this->setClient(); | ||
} | ||
|
||
protected function beforeHandle() | ||
{ | ||
} | ||
|
||
protected function setTimeOut() | ||
{ | ||
switch ($this->frequency) { | ||
case 'hourly': | ||
$this->timeOutAt = Carbon::now()->addHour(1)->timestamp; | ||
break; | ||
case 'daily': | ||
$this->timeOutAt = Carbon::tomorrow()->startOfDay()->timestamp; | ||
break; | ||
case 'monthly': | ||
$this->timeOutAt = (new Carbon('first day of next month'))->firstOfMonth()->startOfDay()->timestamp; | ||
break; | ||
case 'annually': | ||
$this->timeOutAt = (new Carbon('next year'))->startOfYear()->firstOfMonth()->startOfDay()->timestamp; | ||
break; | ||
} | ||
} | ||
|
||
protected function setRequest($request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
protected function setLimit($limit) | ||
{ | ||
$this->limit = $limit; | ||
} | ||
|
||
protected function setFrequency($frequency) | ||
{ | ||
$this->frequency = $frequency; | ||
} | ||
|
||
protected function setClient() | ||
{ | ||
$this->client = new Client($this->request); | ||
} | ||
|
||
final protected function getAllowedBots() | ||
{ | ||
if ($this->options->use_default_allowed_bots) { | ||
return array_merge($this->options->allowed_bots, $this->allowedBots); | ||
} | ||
return $this->allowedBots; | ||
} | ||
|
||
final protected function setAllowedBots($bots) | ||
{ | ||
$this->allowedBots = $bots; | ||
} | ||
|
||
final protected function isLimitExceeded() | ||
{ | ||
return $this->hits > $this->limit; | ||
} | ||
|
||
final protected function isTheFirstOverflow() | ||
{ | ||
return $this->hits === $this->limit + 1; | ||
} | ||
|
||
abstract protected function countHits(); | ||
|
||
abstract protected function isAllowed(); | ||
|
||
abstract protected function notAllowed(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Potelo\LaravelBlockBots\Contracts; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
|
||
class Client | ||
{ | ||
public $id; | ||
public $ip; | ||
public $userAgent; | ||
|
||
public function __construct($request) | ||
{ | ||
$this->id = Auth::check() ? Auth::id() : $this->ip; | ||
$this->ip = $request->getClientIp(); | ||
$this->userAgent = $request->header('User-Agent'); | ||
$this->key = "block_bot:{$this->id}"; | ||
$this->logKey = "block_bot:notified:{$this->ip}"; | ||
$this->url = substr($request->fullUrl(), strlen($request->getScheme() . "://")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Potelo\LaravelBlockBots\Contracts; | ||
|
||
class Configuration | ||
{ | ||
public function __construct() | ||
{ | ||
foreach (config('block-bots') as $key => $value) { | ||
$this->{$key} = $value; | ||
} | ||
} | ||
} |
Empty file.
Oops, something went wrong.