Skip to content

Instantly share code, notes, and snippets.

@lukevers
Last active October 27, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukevers/9837c507bf17345a9962db1b7029f2fe to your computer and use it in GitHub Desktop.
Save lukevers/9837c507bf17345a9962db1b7029f2fe to your computer and use it in GitHub Desktop.
Zbig Labacz
<?php
require_once 'Db.php';
class apiController
{
protected $db;
/**
* Db
* @return mixed
*/
protected function getDb()
{
if (isset($db)) {
$this->db = new Db();
}
return $db;
}
/**
* Saves url
* @param $userToken
* @param $url
* @return bool
* @throws Exception
*
*/
public function saveUrl($userToken, $url)
{
$result = false;
if ($this->getDb()->userExists($userToken)) {
$result = $this->getDb()->addUrl($userToken, $url);
} else {
$result = $this->getDb()
->createUser($userToken)
->addUrl($userToken, $url);
}
return $result;
}
/**
* Gets user's urls
* @param $userToken
* @return mixed
* @throws Exception
*/
public function getUrls($userToken)
{
if ($this->getDb()->userExists($userToken)) {
return $this->getDb()->getUserUrls($userToken);
} else {
throw new Exception("Sorry, the user does not exist");
}
}
/**
* Removes url
* @param $userToken
* @param $url
* @return mixed
* @throws Exception
*/
public function removeUrls($userToken, $url)
{
if ($this->getDb()->userExists($userToken)) {
return $this->getDb()->removeUrl($userToken, $url);
} else {
throw new Exception("Sorry, the user does not exist");
}
}
/**
* Users per domain
* @param $domain
* @return mixed
*/
public function getUsersByDomain($domain)
{
return $this->getDb()->getUsersByDomain($domain);
}
}
<?php
class Db
{
/**
* Users
* @var array
*/
protected $users;
/**
* Domain registry
* @var array
*/
protected $domains;
/**
* Db constructor.
*/
public function __construct()
{
$this->users = [];
}
/**
* Creates a user
* @param $token
*/
public function createUser($token)
{
$this->users[$token] = [
'urls' => []
];
}
/**
* Check if user exists
* @param $token
* @return bool
*/
public function userExists($token)
{
return isset($this->users[$token]);
}
/**
* Add Url
* @param $token
* @param $url
* @return bool
* @throws Exception
*/
public function addUrl($token, $url)
{
if (isset($this->users[$token])) {
$user = & $this->users[$token];
if (!in_array($url, $user['urls'])) {
$user['urls'][sha1($url)] = $url;
$this->buildDomainRegistry($token, $url);
return true;
}
else {
return false;
}
} else {
throw new Exception("User does not exists");
}
}
/**
* User Urls
* @param $userToken
* @return array
*/
public function getUserUrls($userToken)
{
return array_values($this->users[$userToken]['urls']);
}
/**
* Removes url
* @param $userToken
* @param $url
* @return bool
*/
public function removeUrl($userToken, $url)
{
$user = & $this->users[$userToken];
$urlSum = sha1($url);
if (isset($user['urls'][$urlSum])) {
unset($user['urls'][$urlSum]);
return true;
} else {
return false;
}
}
/**
* Domain
* @param $domain
* @return array
*/
public function getUsersByDomain($domain)
{
if (isset($this->domains[$domain])) {
return array_values($this->domains[$domain]);
} else {
return [];
}
}
/**
* Get Domain by Url
* @param $url
* @return string
*/
protected function getDomain($url)
{
return preg_replace("/^(www\.)?(\w+)\.(\w+)/", "$2.$3", $url);
}
/**
* Build domain registry
* @param $userToken
* @param $url
*
* @return $this
*/
protected function buildDomainRegistry($userToken, $url)
{
$domain = $this->getDomain($url);
if (!isset($this->domains[$domain])) {
$this->domains[$domain] = [];
}
if (!isset($this->domains[$domain][$userToken])) {
$this->domains[$domain][$userToken] = 1;
} else {
$this->domains[$domain][$userToken]++;
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment