Skip to content

Commit

Permalink
some psr-12 and clean code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
krysits committed Sep 9, 2020
1 parent fe09f5f commit 7749628
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 80 deletions.
2 changes: 1 addition & 1 deletion public/front.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<ins data-revive-zoneid="3" data-revive-id="0acc88b3a59a820af783b96c26cb2f66"></ins>
<?= $_SERVER['SERVER_PORT'] == 443 ? '<script async src="https://node.lv/www/delivery/asyncjs.php"></script>':'';?>

<p class="text-muted small mt-2 mb-lg-0">&copy; <a href="https://krysits.com/" target="_blank">krysits.com</a> <?php echo date('Y');?></p>
<p class="text-muted small mt-2 mb-lg-0">&copy; <a href="https://0k.lv/krysits" target="_blank">krysits.com</a> <?php echo date('Y');?></p>
</div>
</div>
</div>
Expand Down
60 changes: 32 additions & 28 deletions src/Shortener/App.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Shortener;

use Exception;
use Pecee\Http\Exceptions\MalformedUrlException;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Shortener\Models\Url;
Expand All @@ -25,37 +26,40 @@ public function checkIP() {}

public function redirect($uri='')
{
if(empty($uri)) return;
if(empty($uri)) {
return;
}
header("Location: " . $uri);
}

// routes
public function setRoutes() {

Router::get('/{code}', function ($code) {
$goUrl = (new Url)->getRecords(['code'=>$code], ['url', 'id'], 1);
$aliasUrl = (new Url)->getRecords(['alias'=>$code], ['url', 'id'], 1);

if($goUrl) {
new Hit($goUrl[0]->id);
$this->redirect($goUrl[0]->url);
}
else if($aliasUrl) {
new Hit($aliasUrl[0]->id);
$this->redirect($aliasUrl[0]->url);
}
else {
$this->redirect('/');
}

})->where(['code'=>'[A-Za-z]+']);

Router::get('/', static function () {
include_once 'front.php';
return '';
});

}
try {
Router::get('/{code}', function ($code) {
$goUrl = (new Url)->getRecords(['code' => $code], ['url', 'id'], 1);
$aliasUrl = (new Url)->getRecords(['alias' => $code], ['url', 'id'], 1);

if ($goUrl) {
new Hit($goUrl[0]->id);
$this->redirect($goUrl[0]->url);
} elseif ($aliasUrl) {
new Hit($aliasUrl[0]->id);
$this->redirect($aliasUrl[0]->url);
} else {
$this->redirect('/');
}
})->where(['code' => '[A-Za-z]+']);
} catch (MalformedUrlException $e) {
}

try {
Router::get('/', static function () {
include_once 'front.php';
return '';
});
} catch (MalformedUrlException $e) {
}
}

public function run()
{
Expand All @@ -64,7 +68,7 @@ public function run()
Router::start();
} catch (MalformedUrlException $e) {
} catch (HttpException $e) {
} catch (\Exception $e) {
} catch (Exception $e) {
}
}
};
}
37 changes: 0 additions & 37 deletions src/Shortener/Auth.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Shortener/CountryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function getCCbyIP($ip = Null)
private function _geo_loc_code($ip){
$url = $this->_ip_url . $ip . '/json';
$data = file_get_contents($url);
$json_data = json_decode($data);
$json_data = json_decode($data, false);

if(!empty($json_data->country)) {
return $json_data->country;
}

return $this->country_code; // old value
}
};
}
4 changes: 2 additions & 2 deletions src/Shortener/Models/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct($url_id = 0)
// methods
public function setTable($table = '')
{
$this->_table = (empty($table)) ? strtolower($this->getTableNameByNamespace(get_class())) : $table;
$this->_table = (empty($table)) ? strtolower($this->getTableNameByNamespace(__CLASS__)) : $table;
}

public function addHit($url_id = 0) {
Expand All @@ -53,4 +53,4 @@ public function addHit($url_id = 0) {

return $this->save((array) $this);
}
};
}
14 changes: 4 additions & 10 deletions src/Shortener/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ class Url extends Model
public $deleted_at; //date('Y-m-d H:i:s');

// constructor
public function __construct()
{
parent::__construct();
}

// methods
// methods
public function setTable($table = '')
{
$this->_table = (empty($table)) ? strtolower($this->getTableNameByNamespace(get_class())) : $table;
$this->_table = (empty($table)) ? strtolower($this->getTableNameByNamespace(__CLASS__)) : $table;
}

public function generate_code($number) {
Expand Down Expand Up @@ -81,11 +76,10 @@ public function addNew()

public function getMaxId()
{
$sql = "SELECT max(id) as number FROM ";
$sql .= $this->_table;
$sql = "SELECT max(id) as number FROM " . $this->_table;

$result = $this->_db->query($sql);

return $result->fetchColumn() ?: 0;
}
};
}

0 comments on commit 7749628

Please sign in to comment.