Skip to content

Commit

Permalink
Tidied up codebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
SidRoberts committed Apr 13, 2019
1 parent cdbbce5 commit 3503fd0
Show file tree
Hide file tree
Showing 63 changed files with 2,127 additions and 634 deletions.
60 changes: 42 additions & 18 deletions Library/Phalcon/Avatar/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Gravatar implements Avatarable
'monsterid' => true,
'wavatar' => true,
'retro' => true,
'blank' => true
'blank' => true,
];

/**
Expand All @@ -115,7 +115,7 @@ class Gravatar implements Avatarable
self::RATING_G => true,
self::RATING_PG => true,
self::RATING_R => true,
self::RATING_X => true
self::RATING_X => true,
];

/**
Expand Down Expand Up @@ -149,19 +149,27 @@ public function __construct($config)
}

if (!is_array($config)) {
throw new InvalidArgumentException('Config must be either an array or \Phalcon\Config instance');
throw new InvalidArgumentException(
'Config must be either an array or \Phalcon\Config instance'
);
}

if (isset($config['default_image'])) {
$this->setDefaultImage($config['default_image']);
$this->setDefaultImage(
$config['default_image']
);
}

if (isset($config['rating'])) {
$this->setRating($config['rating']);
$this->setRating(
$config['rating']
);
}

if (isset($config['size'])) {
$this->setSize($config['size']);
$this->setSize(
$config['size']
);
}

if (isset($config['use_https']) && $config['use_https']) {
Expand Down Expand Up @@ -232,16 +240,18 @@ public function setSize($size)
$options = [
'options' => [
'min_range' => static::MIN_AVATAR_SIZE,
'max_range' => static::MAX_AVATAR_SIZE
'max_range' => static::MAX_AVATAR_SIZE,
]
];

if (false === filter_var($size, FILTER_VALIDATE_INT, $options)) {
throw new InvalidArgumentException(sprintf(
"Can't set Gravatar size. Size must be an integer within %s and %s pixels",
static::MIN_AVATAR_SIZE,
static::MAX_AVATAR_SIZE
));
throw new InvalidArgumentException(
sprintf(
"Can't set Gravatar size. Size must be an integer within %s and %s pixels",
static::MIN_AVATAR_SIZE,
static::MAX_AVATAR_SIZE
)
);
}

$this->size = (int) $size;
Expand Down Expand Up @@ -271,14 +281,18 @@ public function setRating($rating)
{
$rating = strtolower(trim($rating));


if (!isset($this->validRatings[$rating])) {
$allowed = array_keys($this->validRatings);
$last = array_pop($allowed);
$allowed = join(',', $allowed);

throw new InvalidArgumentException(
sprintf("Invalid rating '%s' specified. Available for use only: %s or %s", $rating, $allowed, $last)
sprintf(
"Invalid rating '%s' specified. Available for use only: %s or %s",
$rating,
$allowed,
$last
)
);
}

Expand Down Expand Up @@ -407,18 +421,28 @@ protected function buildURL($email)

$query = [
's' => $this->getSize(),
'r' => $this->getRating()
'r' => $this->getRating(),
];

if ($this->defaultImage) {
$query = array_merge($query, ['d' => $this->defaultImage]);
$query = array_merge(
$query,
[
'd' => $this->defaultImage,
]
);
}

if ($this->forceDefault) {
$query = array_merge($query, ['f' => 'y']);
$query = array_merge(
$query,
[
'f' => 'y',
]
);
}

$url .= '?'.http_build_query($query, '', '&');
$url .= '?' . http_build_query($query, '', '&');

return $url;
}
Expand Down
82 changes: 60 additions & 22 deletions Library/Phalcon/Cache/Backend/Aerospike.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,31 @@
* use Phalcon\Cache\Backend\Aerospike as CacheBackend;
*
* // Cache data for 2 days
* $frontCache = new Data(['lifetime' => 172800]);
* $frontCache = new Data(
* [
* 'lifetime' => 172800,
* ]
* );
*
* // Create the Cache setting redis connection options
* $cache = new CacheBackend($frontCache, [
* 'hosts' => [
* ['addr' => '127.0.0.1', 'port' => 3000]
* ],
* 'persistent' => true,
* 'namespace' => 'test',
* 'prefix' => 'cache_',
* 'options' => [
* \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
* \Aerospike::OPT_WRITE_TIMEOUT => 1500
* $cache = new CacheBackend(
* $frontCache,
* [
* 'hosts' => [
* [
* 'addr' => '127.0.0.1',
* 'port' => 3000,
* ],
* ],
* 'persistent' => true,
* 'namespace' => 'test',
* 'prefix' => 'cache_',
* 'options' => [
* \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
* \Aerospike::OPT_WRITE_TIMEOUT => 1500,
* ]
* ]
* ]);
* );
*
* // Cache arbitrary data
* $cache->save('my-data', [1, 2, 3, 4, 5]);
Expand Down Expand Up @@ -97,6 +107,7 @@ public function __construct(FrontendInterface $frontend, array $options)

if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];

unset($options['namespace']);
}

Expand All @@ -106,6 +117,7 @@ public function __construct(FrontendInterface $frontend, array $options)

if (isset($options['set']) && !empty($options['set'])) {
$this->set = $options['set'];

unset($options['set']);
}

Expand All @@ -119,11 +131,21 @@ public function __construct(FrontendInterface $frontend, array $options)
$opts = $options['options'];
}

$this->db = new \Aerospike(['hosts' => $options['hosts']], $persistent, $opts);
$this->db = new \Aerospike(
[
'hosts' => $options['hosts'],
],
$persistent,
$opts
);

if (!$this->db->isConnected()) {
throw new Exception(
sprintf('Aerospike failed to connect [%s]: %s', $this->db->errorno(), $this->db->error())
sprintf(
'Aerospike failed to connect [%s]: %s',
$this->db->errorno(),
$this->db->error()
)
);
}

Expand Down Expand Up @@ -185,12 +207,17 @@ public function save($keyName = null, $content = null, $lifetime = null, $stopBu
$aKey,
$bins,
$lifetime,
[\Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND]
[
\Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND,
]
);

if (\Aerospike::OK != $status) {
throw new Exception(
sprintf('Failed storing data in Aerospike: %s', $this->db->error()),
sprintf(
'Failed storing data in Aerospike: %s',
$this->db->error()
),
$this->db->errorno()
);
}
Expand Down Expand Up @@ -225,13 +252,24 @@ public function queryKeys($prefix = null)
$keys = [];
$globalPrefix = $this->_prefix;

$this->db->scan($this->namespace, $this->set, function ($record) use (&$keys, $prefix, $globalPrefix) {
$key = $record['key']['key'];

if (empty($prefix) || 0 === strpos($key, $prefix)) {
$keys[] = preg_replace(sprintf('#^%s(.+)#u', preg_quote($globalPrefix)), '$1', $key);
$this->db->scan(
$this->namespace,
$this->set,
function ($record) use (&$keys, $prefix, $globalPrefix) {
$key = $record['key']['key'];

if (empty($prefix) || 0 === strpos($key, $prefix)) {
$keys[] = preg_replace(
sprintf(
'#^%s(.+)#u',
preg_quote($globalPrefix)
),
'$1',
$key
);
}
}
});
);

return $keys;
}
Expand Down
Loading

0 comments on commit 3503fd0

Please sign in to comment.