Skip to content

Instantly share code, notes, and snippets.

@ineersa
Created June 14, 2015 09:10
Show Gist options
  • Save ineersa/fc7601f7b1950c75e817 to your computer and use it in GitHub Desktop.
Save ineersa/fc7601f7b1950c75e817 to your computer and use it in GitHub Desktop.
<?php
namespace TrafficTest;
class Traffic
{
private $db;
private $table = 'traffic';
public $code;
public $period;
public $raw_count;
public function __construct()
{
$this->db = new \PDO('mysql:host=localhost;dbname=oneffect_demo;charset=utf8', 'root', 'admin');
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
}
public function rules()
{
return [
['code','required'],
['code','length','max',10],
['code','sanitize']
];
}
public function validate()
{
foreach($this->rules() as $rule){
list($attribute,$validator,$validator_rule,$validator_rule_value) = $rule;
if (!call_user_func([$this,$validator.'Validator'],
$attribute, $validator_rule, $validator_rule_value))
return false;
}
return true;
}
public function requiredValidator($attribute)
{
if (is_null($this->$attribute) || $this->$attribute ==='' || $this->$attribute === false){
return false;
}
return true;
}
public function sanitizeValidator($attribute)
{
$this->$attribute = htmlspecialchars($this->$attribute);
return true;
}
public function lengthValidator($attribute, $validator_rule, $validator_rule_value)
{
$length = mb_strlen($this->$attribute,'UTF-8');
switch ($validator_rule){
case 'max':
if ($length>$validator_rule_value)
return false;
break;
case 'min':
if ($length<$validator_rule_value)
return false;
break;
default:
break;
}
return true;
}
public function codeExists()
{
$stmt = $this->db->prepare(
"SELECT 1
FROM {$this->table}
WHERE code=? AND period=?
LIMIT 1
"
);
$stmt->execute([$this->code,$this->period]);
$result = $stmt->fetchColumn(0);
return $result;
}
public function getCounter()
{
$stmt = $this->db->prepare(
"SELECT raw_count
FROM {$this->table}
WHERE code=? AND period=?
LIMIT 1
"
);
$stmt->execute([$this->code,$this->period]);
$result = $stmt->fetchColumn(0);
return $result;
}
public function incrementTrafficCode()
{
if ($this->codeExists()){
try {
$stmt = $this->db->prepare("
UPDATE {$this->table}
SET raw_count=raw_count+1
WHERE code=? AND period=?
LIMIT 1;
");
$stmt->execute([$this->code,$this->period]);
return $stmt->rowCount();
} catch(\PDOException $ex) {
some_logging_function($ex->getMessage());
}
} else {
try {
$stmt = $this->db->prepare("
INSERT INTO {$this->table}(code,period,raw_count)
VALUES(?,?,1)");
$stmt->execute([$this->code,$this->period]);
return 1;
} catch(\PDOException $ex) {
some_logging_function($ex->getMessage());
}
}
return false;
}
}
$model = new \TrafficTest\Traffic;
$model->code = $_GET['code'];
$model->period = date('Y-m-d');
if ($model->validate()){
$count = $model->incrementTrafficCode();
echo "{$count} row affected!\r\n";
} else echo "Bad Input";
echo "Code used - ".$model->getCounter()." today";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment